Reload a previously imported module. The argument must
be a module object, so it must have been successfully imported before.
This is useful if you have edited the module source file using an
external editor and want to try out the new version without leaving
the Python interpreter. The return value is the module object (the
same as the module argument).
>>> oldmods = set(sys.modules.keys())
>>> import script
>>> # Do stuff
>>> for mod in set(sys.modules.keys()).difference(oldmods): sys.modules.pop(mod)
>>> import script
http://docs.python.org/library/functions.html#reload
您可以发出一个
reload script
,但这不会更新您现有的对象,也不会深入到其他模块中。幸运的是,这是通过
IPython
解决的,这是一个更好的python shell,支持自动重新加载。要在
IPython
中使用autoreloading,您必须首先键入import ipy_autoreload
,或者将其永久放入~/.ipython/ipy_user_conf.py
。然后运行:
%autoreload 1
意味着加载了%aimport
的每个模块都将在从提示符执行代码之前重新加载。但是,这不会更新任何现有对象。请参阅http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html以了解您可以做的更多有趣的事情。
另一个对我有很大帮助的解决方案是维护
sys.modules
键的副本,并在导入后弹出新模块以强制重新导入深层导入:相关问题 更多 >
编程相关推荐