1、Python2中可以和Python3中關於reload()用法的區別。
Python2 中可以直接使用reload(module)重載模塊。
Pyhton3中需要使用如下兩種方式:
方式(1)
>>> from imp
>>> imp.reload(module)
方式(2)
>>> from imp import reload
>>> reload(module)
2、Python中使用import和reload()出現錯誤的原因
在Python中,以py為擴展名保存的文件就可以認為是一個模塊,模塊包含了 Python 對象定義和Python語句。
假設recommendations.py 放在C:\Python34\PCI_Code\chapter2\目錄下,其中包含函數critics
如果在import函數的時候出現如下錯誤:
>>> from recommendation import critics Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> from recommendation import critics ImportError: No module named 'recommendation'
請把目錄C:\Python34\PCI_Code\chapter2\加到系統路徑中,
>>> import sys >>> sys.path.append("C:\Python34\PCI_Code\chapter2") >>> from recommendations import critics >>>
或者切換到文件所在的目錄中,
C:\Python34\PCI_Code\chapter2>python >>> from recommendations import * >>>
------------------------------------------------------------------------------------------------------------------------------------
使用reload()時出現如下錯誤
使用reload()時出現如下錯誤
>>> from imp import reload >>> reload(recommendations) Traceback (most recent call last): File "<pyshell#86>", line 1, in <module> reload(recommendations) NameError: name 'recommendations' is not defined原因是因為在reload某個模塊的時候,需要先import來加載需要的模塊,這時候再去reload就不會有問題,具體看下面代碼:
>>> from imp import reload >>> import recommendations >>> reload(recommendations) <module 'recommendations' from 'C:\\Python34\\PCI_Code\\chapter2\\recommendations.py'> >>>
出處:http://www.itwendao.com/article/detail/93294.html
==================================================================================
Python模塊詳細說明:> http://www.runoob.com/python/python-modules.html