我经常在Python解释器中测试我的模块,当我看到错误时,我会快速更新.py文件。但是我如何让它反映在解释器上呢?所以,到目前为止,我一直在退出并重新进入解释器,因为重新导入文件再次不适合我。


当前回答

Python3的更新:(引用自已经回答的答案,因为这里的最后一个编辑/注释建议使用一个已弃用的方法)

在python3中,reload被移动到imp模块。在3.4中,弃用了imp,改用了importlib,并在后者中添加了reload。当目标为3或更高版本时,在调用reload或import时引用适当的模块。

导读:

Python3 >= 3.4: importlib.reload(packagename) Python3 < 3.4: imp.reload(packagename) Python2:继续


使用重载内置函数:

https://docs.python.org/2/library/functions.html#reload

When reload(module) is executed: Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time. As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero. The names in the module namespace are updated to point to any new or changed objects. Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

例子:

# Make a simple function that prints "version 1"
shell1$ echo 'def x(): print "version 1"' > mymodule.py

# Run the module
shell2$ python
>>> import mymodule
>>> mymodule.x()
version 1

# Change mymodule to print "version 2" (without exiting the python REPL)
shell2$ echo 'def x(): print "version 2"' > mymodule.py

# Back in that same python session
>>> reload(mymodule)
<module 'mymodule' from 'mymodule.pyc'>
>>> mymodule.x()
version 2

其他回答

import sys
del sys.modules['module_name']

基本上是在allyourcode的asnwer中重新加载。但它不会改变已经实例化的对象或引用函数的底层代码。从他的回答延伸出来:

#Make a simple function that prints "version 1"
shell1$ echo 'def x(): print "version 1"' > mymodule.py

# Run the module
shell2$ python
>>> import mymodule
>>> mymodule.x()
version 1
>>> x = mymodule.x
>>> x()
version 1
>>> x is mymodule.x
True


# Change mymodule to print "version 2" (without exiting the python REPL)
shell2$ echo 'def x(): print "version 2"' > mymodule.py

# Back in that same python session
>>> reload(mymodule)
<module 'mymodule' from 'mymodule.pyc'>
>>> mymodule.x()
version 2
>>> x()
version 1
>>> x is mymodule.x
False

Python3的更新:(引用自已经回答的答案,因为这里的最后一个编辑/注释建议使用一个已弃用的方法)

在python3中,reload被移动到imp模块。在3.4中,弃用了imp,改用了importlib,并在后者中添加了reload。当目标为3或更高版本时,在调用reload或import时引用适当的模块。

导读:

Python3 >= 3.4: importlib.reload(packagename) Python3 < 3.4: imp.reload(packagename) Python2:继续


使用重载内置函数:

https://docs.python.org/2/library/functions.html#reload

When reload(module) is executed: Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time. As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero. The names in the module namespace are updated to point to any new or changed objects. Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

例子:

# Make a simple function that prints "version 1"
shell1$ echo 'def x(): print "version 1"' > mymodule.py

# Run the module
shell2$ python
>>> import mymodule
>>> mymodule.x()
version 1

# Change mymodule to print "version 2" (without exiting the python REPL)
shell2$ echo 'def x(): print "version 2"' > mymodule.py

# Back in that same python session
>>> reload(mymodule)
<module 'mymodule' from 'mymodule.pyc'>
>>> mymodule.x()
version 2

在Python 3中,行为会发生变化。

>>> import my_stuff

... 用my_stuff做一些事情,然后:

>>>> import imp
>>>> imp.reload(my_stuff)

你会得到一个全新的,重新加载的my_stuff。

不确定这是否完成了所有预期的事情,但你可以这样做:

>>> del mymodule
>>> import mymodule