在IPython中自动加载模块

Tho*_*low 153 python ipython jupyter-notebook

有没有办法让IPython自动重新加载所有更改的代码?在每个行在shell中执行之前,或者在特别请求它时失败.我正在使用IPython和SciPy进行大量的探索性编程,每当我更改它时,必须手动重新加载每个模块是非常痛苦的.

And*_*510 242

对于IPython版本3.1,4.x和5.x.

%load_ext autoreload
%autoreload 2
Run Code Online (Sandbox Code Playgroud)

然后默认情况下会自动重新加载您的模块.这是文档:

File:       ...my/python/path/lib/python2.7/site-packages/IPython/extensions/autoreload.py

Docstring:
``autoreload`` is an IPython extension that reloads modules
automatically before executing the line of code typed.

This makes for example the following workflow possible:

.. sourcecode:: ipython

   In [1]: %load_ext autoreload

   In [2]: %autoreload 2

   In [3]: from foo import some_function

   In [4]: some_function()
   Out[4]: 42

   In [5]: # open foo.py in an editor and change some_function to return 43

   In [6]: some_function()
   Out[6]: 43

The module was reloaded without reloading it explicitly, and the
object imported with ``from foo import ...`` was also updated.
Run Code Online (Sandbox Code Playgroud)

有一个技巧:当你在使用时忘记所有上述内容时ipython,试试:

import autoreload
?autoreload
# Then you get all the above
Run Code Online (Sandbox Code Playgroud)

  • `%autoreload 2` 中的 2 是什么意思? (4认同)
  • “%aut​​oreload 2”中的“2”表示“每次执行键入的 Python 代码之前重新加载所有模块(%aimport 排除的模块除外)” https://ipython.org/ipython-doc/3/config/extensions /自动重载.html (3认同)
  • 有没有办法在ipdb中做到这一点?说,我在ipd中,我注意到一条线没有用。所以我改变了这一行,并想重新加载文件。这样行吗? (2认同)
  • 每次我必须这样做时我都会来这里。这是我无法/不想记住的两行诗 (2认同)
  • 同样在这里@JuanLuisRuiz-tagle,大约 5 年谷歌搜索并来到这个线程哈哈 (2认同)

kar*_*niz 88

如上所述,您需要autoreload扩展名.如果您希望每次启动时自动启动ipython,则需要将其添加到ipython_config.py启动文件中:

可能有必要先生成一个:

ipython profile create
Run Code Online (Sandbox Code Playgroud)

然后将这些行包含在~/.ipython/profile_default/ipython_config.py:

c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')
Run Code Online (Sandbox Code Playgroud)

除了需要在.pyc文件中利用已编译的Python代码之外的可选警告:

c.InteractiveShellApp.exec_lines.append('print "Warning: disable autoreload in ipython_config.py to improve performance." ')
Run Code Online (Sandbox Code Playgroud)

编辑:以上版本适用于版本0.12.1和0.13

  • 使用`c.InteractiveShellApp.extensions = ['autoreload']`和`c.InteractiveShellApp.exec_lines = ['%autoreload 2']`.我不确定但是在Ubuntu 13.04下的0.13版本的默认配置文件中,我找到了一个'startup'文件夹,其中包含一个脚本'50_autoreload.ipy'来激活自动重载.也许根本不需要任何东西 (8认同)
  • 这是一个很好的方法,但我认为您需要做的就是填写应该在第27行附近的扩展:`c.InteractiveShellApp.extensions = ['autoreload']` (6认同)

小智 65

修订 - 请参阅下面的Andrew_1510 答案,因为IPython已经更新.

...

有点难以弄清楚如何从尘土飞扬的错误报告中找到答案,但是:

它现在随附IPython!

import ipy_autoreload
%autoreload 2
%aimport your_mod

# %autoreload? for help
Run Code Online (Sandbox Code Playgroud)

...然后每次打电话your_mod.dwim(),它都会拿起最新版本.

  • 如果不那么直接怎么办?`%run sometest.py`包含`import themod`.在编辑`themod.py`之后,我想只是'%运行sometest.py`,但它没有收到更改. (4认同)
  • 我认为ipython 0.11取消了这个功能.或者它只是重命名/隐藏在某个地方? (2认同)

low*_*ech 9

如果你使用下面的行将ipython_config.py添加到〜/ .ipython/profile_default dir中,那么将在ipython启动时加载自动加载功能(在2.0.0上测试):

print "--------->>>>>>>> ENABLE AUTORELOAD <<<<<<<<<------------"

c = get_config()
c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')
Run Code Online (Sandbox Code Playgroud)