在解释器中重新加载(更新)模块文件 [英] reload (update) a module file in the interpreter
问题描述
假设我有这个python脚本script.py
,然后通过键入将其加载到解释器中
Let's say I have this python script script.py
and I load it in the interpreter by typing
import script
然后输入以下内容来执行我的功能:
and then I execute my function by typing:
script.testFunction(testArgument)
到目前为止,还算不错,但是当我更改script.py
时,如果我尝试再次导入,则脚本不会更新.我必须退出解释器,重新启动解释器,然后导入新版本的脚本才能使其正常工作.
OK so far so good, but when I change script.py
, if I try to import again the script doesn't update. I have to exit from the interpreter, restart the interpreter, and then import the new version of the script for it to work.
我该怎么办?
推荐答案
您可以发出reload script
,但这不会更新现有对象,也不会深入其他模块内部.
You can issue a reload script
, but that will not update your existing objects and will not go deep inside other modules.
幸运的是,此问题已通过IPython
解决-更好的python shell,支持自动重新加载.
Fortunately this is solved by IPython
- a better python shell which supports auto-reloading.
要在IPython
中使用自动重载,您必须先键入import ipy_autoreload
,或将其永久放置在~/.ipython/ipy_user_conf.py
中.
To use autoreloading in IPython
, you'll have to type import ipy_autoreload
first, or put it permanently in your ~/.ipython/ipy_user_conf.py
.
然后运行:
%autoreload 1
%aimport script
%autoreload 1
表示,在从提示执行代码之前,将重新加载每个装有%aimport
的模块.但是,这将不更新任何现有对象.
%autoreload 1
means that every module loaded with %aimport
will be reloaded before executing code from the prompt. This will not update any existing objects, however.
请参见 http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html ,以获得更多有趣的操作.
See http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html for more fun things you can do.
这篇关于在解释器中重新加载(更新)模块文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!