我正在运行 python 3.6.4
(anaconda、spyder)。
我是否需要重新加载用户定义的模块以捕获更改?
例如,假设我编写了简单的函数并将其保存在test.py
文件中:
def plus5(x):
return x + 5
然后在 IPython 控制台中输入
import test as t
然后我将用户定义的函数更改为:
def plus5(x):
return x + 500
然后当我在 IPython 控制台中输入
t.plus5(0)
它返回 500 而无需首先重新导入或重新加载模块。
如果我将函数名称从 plus5
更改为其他名称,那么我必须重新导入模块才能看到更改。但是当我更改函数语句时,它会自动捕获更改而无需重新导入模块
来自 Python 文档:
Note: For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use
importlib.reload()
e.g.
import importlib; importlib.reload(modulename)
.
最佳答案
这是 IPython 解释器名称中的一个特性 autoreload
.它有魔法命令 %autoreload
允许激活或停用此功能。它似乎是默认打开的,但我找不到证明这一点的东西。
关于python - 我是否必须在 python 中重新加载模块才能捕获更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268137/