IPython/jupyter notebook 中的自动更新模块

autoupdate module in IPython / jupyter notebook

我写了自己的模块,结构如下:

mymodule/
├── __init__.py
├── part1.py
├── part2.py
├── part3.py
└── part4.py

为了测试我的模块,我正在使用 IPython and/or jupyter notebook(以前称为 Ipython Notebook)。像往常一样,我像

一样进行模块导入
import mymodule

假设我在 part2.py 中编辑了一些代码并想使用我的模块的更新版本。首先,我认为只要按 import mymodule 重新导入模块就可以完成这项工作,但事实并非如此。要完全重新加载模块,我必须关闭 IPython 的 shell 或重新启动 jupyter 的内核并通过导入 mymodule.

重新启动

不过,参考docs,IPython提供了一个名为autoreload的自动更新功能,它提供了不同的模式,可以按如下方式激活:

%load_ext autoreload
%autoreload 1
%aimport mymodule

使用我的两个片段,我正在导入 mymodule 这样的:

%load_ext autoreload
%autoreload 1
%aimport mymodule

import mymodule

# let's do something with the module here

然而,即使激活了 autoreload 1autoreload 2,IPython 和 jupyter 都没有按照我的期望去做,我仍然不得不退出 IPython's shell 或重新启动 jupyter 的内核以使用 part2.py 的编辑代码,它是 mymodule.

的一部分

我做错了什么?看来我没明白这应该如何工作。

如 dashesy 所说,将 %autoreload 1 更改为 %autoreload 2,旧版本或错误。