重新加载Python函数

3
我是一个Jupyter Lab笔记本的用户,尝试修改代码并在Jupyter Notebook中重新加载它,而无需重新加载内核即可使用修改后的代码。我正在使用Python 3.5.5,并运行以下代码:
(在test.py文件中)
def myTest():
    print('hello')

(在jupyter中)

from test import myTest
import importlib
importlib.reload(test)
myTest()

在我的jupyter lab笔记本中运行代码时,我得到了一个NameError错误,提示name 'test' is not defined。通过在stackoverflow上搜索,我发现这种错误唯一的参考是使用旧版本的Python时会出现问题。但我正在使用的importlib.reload()似乎是正确的。


1
你在哪里定义了 testmyTest 似乎已经被定义了。 - ChootsMagoots
也许你执行了 from test import myTest - Jean-François Fabre
抱歉,忘记了那部分内容;问题已修复。 - The Nightman
1个回答

9
你尝试过内置的魔术命令autoreload吗?
在你的笔记本开头添加以下内容:
%load_ext autoreload
%autoreload 2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接