从另一个 jupyter notebook 导入函数后,在该函数中所做的更改不会反映在导入它的 notebook 中。两个笔记本都在同一个 directory
中。
假设我有 2 个笔记本 - Notebook_1.ipynb
和 Notebook_2.ipynb
,hello_world()
中有一个函数 Notebook_2.ipynb
-
# My Notebook_2.ipynb
def hello_world():
print('Hello there')
现在,我在 hello_world()
中导入 Notebook_1.ipynb
:
# This is Notebook_1
from ipynb.fs.full.Notebook_2 import hello_world
hello_world()
Helloo there
现在,让我将 Notebook_2.ipynb
从 Hello there
更改为 Hello now
和 saved
文件:
# My Notebook_2.ipynb
def hello_world():
print('Hello now')
我读到 here
,因为模块已经导入,所以它混淆了 python 并且它不会重新加载它。因此,一种方法是重新启动内核,但随后我们可能会丢失其他信息。另一种方法是使用 importlib.reload(Notebook_2.ipynb)
。我这样做了,但我仍然得到 Hello there
而不是 Hello now
。等了 5 分钟后,我得到了 Hello now
。
importlib.reload(ipynb.fs.full.Notebook_2)
hello_world()
Helloo there
如何解决这个问题,让更新后的功能立即体现出来?