如何“重新导入”模块到python然后在导入后更改代码 [英] how to "reimport" module to python then code be changed after import
本文介绍了如何“重新导入”模块到python然后在导入后更改代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 foo.py
def foo():
print "test"
在IPython中我使用:
In IPython I use:
In [6]: import foo
In [7]: foo.foo()
test
然后我将 foo()
更改为:
def foo():
print "test changed"
在IPython中,调用的结果仍然是 test
:
In IPython, the result for invoking is still test
:
In [10]: import foo
In [11]: foo.foo()
test
然后我使用:
In [15]: del foo
In [16]: import foo
In [17]: foo.foo()
test
我删除同一文件夹 foo.py
中的 foo.pyc
,但仍然没有运气。
I delete the foo.pyc
in same folder foo.py
exists, but still no luck.
我是否知道如何在运行时重新导入更新的代码?
May I know how to reimport the updated code in runtime?
推荐答案
对于Python 2.x
For Python 2.x
reload(foo)
对于Python 3.x
For Python 3.x
import importlib
import foo #import the module here, so that it can be reloaded.
importlib.reload(foo)
这篇关于如何“重新导入”模块到python然后在导入后更改代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文