【发布时间】:2010-12-26 20:26:12
【问题描述】:
有没有办法让 IPython 自动重新加载所有更改的代码?在每行在 shell 中执行之前或在特别要求时失败。我正在使用 IPython 和 SciPy 进行大量探索性编程,每次更改时都必须手动重新加载每个模块,这非常痛苦。
【问题讨论】:
-
您可以考虑更改接受的答案。
标签: python jupyter-notebook ipython
有没有办法让 IPython 自动重新加载所有更改的代码?在每行在 shell 中执行之前或在特别要求时失败。我正在使用 IPython 和 SciPy 进行大量探索性编程,每次更改时都必须手动重新加载每个模块,这非常痛苦。
【问题讨论】:
标签: python jupyter-notebook ipython
【讨论】:
已修订 - 请参阅下方 Andrew_1510 的 answer,因为 IPython 已更新。
...
从尘土飞扬的错误报告中弄清楚如何到达那里有点困难,但是:
它现在随 IPython 一起提供!
import ipy_autoreload
%autoreload 2
%aimport your_mod
# %autoreload? for help
...那么每次您拨打your_mod.dwim(),它都会获取最新版本。
【讨论】:
%run sometest.py 包含 import themod。编辑themod.py 后,我想只写%run sometest.py,但它不会接受更改。
对于 IPython 版本 3.1、4.x 和 5.x
%load_ext autoreload
%autoreload 2
那么您的模块将默认自动重新加载。这是文档:
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.
有个窍门:当你在使用ipython时忘记所有以上的,试试看:
import autoreload
?autoreload
# Then you get all the above
【讨论】:
ipdb 中做到这一点?说,我在 ipd 中,我注意到一行没有工作。所以我改变了行,并想重新加载文件。这行得通吗?
if 'autoreload' not in get_ipython().extension_manager.loaded:\n %load_ext autoreload\n %autoreload 2。这样就可以摆脱再次执行命令时出现的如下错误:The autoreload extension is already loaded. To reload it, use:\n %reload_ext autoreload。
%autoreload 2 中的 2 是什么意思?
%autoreload 2 中的2 表示Reload all modules (except those excluded by %aimport) every time before executing the Python code typed. ipython.org/ipython-doc/3/config/extensions/autoreload.html
如上所述,您需要autoreload 扩展名。如果你想让它在每次启动ipython时自动启动,你需要将它添加到ipython_config.py启动文件中:
可能需要先生成一个:
ipython profile create
然后在~/.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')
还有一个可选警告,以防您需要利用 .pyc 文件中的已编译 Python 代码:
c.InteractiveShellApp.exec_lines.append('print("Warning: disable autoreload in ipython_config.py to improve performance.")')
编辑:以上适用于版本 0.12.1 和 0.13
【讨论】:
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.extensions = ['autoreload']和c.InteractiveShellApp.exec_lines = ['%autoreload 2']。我不确定,但在 Ubuntu 13.04 下版本 0.13 的默认配置文件中,我发现了一个“启动”文件夹,其中包含一个脚本“50_autoreload.ipy”来激活自动重载。也许什么都不需要
你可以使用:
import ipy_autoreload
%autoreload 2
%aimport your_mod
【讨论】:
如果您将文件 ipython_config.py 添加到 ~/.ipython/profile_default 目录中,如下所示,则自动重载功能将在 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')
【讨论】: