IPython 中的模块自动重新加载
有没有办法让 IPython 自动重新加载所有更改的代码?要么在 shell 中执行每一行之前,要么在特别请求时失败。我正在使用 IPython 和 SciPy 进行大量探索性编程,每次更改模块时都必须手动重新加载每个模块,这非常痛苦。
Is there a way to have IPython automatically reload all changed code? Either before each line is executed in the shell or failing that when it is specifically requested to. I'm doing a lot of exploratory programming using IPython and SciPy and it's quite a pain to have to manually reload each module whenever I change it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于 IPython 版本 3.1、4.x 和 5.x
那么您的模块将默认自动重新加载。这是文档:
有一个技巧:当您在使用 ipython 时忘记了以上所有内容时,只需尝试:
For IPython version 3.1, 4.x, and 5.x
Then your module will be auto-reloaded by default. This is the doc:
There is a trick: when you forget all of the above when using
ipython
, just try:如上所述,您需要
autoreload
扩展。如果你想让它在每次启动ipython
时自动启动,你需要将其添加到ipython_config.py
启动文件中:可能需要先生成一个:
然后在
~/.ipython/profile_default/ipython_config.py
中包含这些行:以及可选警告,以防您需要利用
.pyc
文件中已编译的 Python 代码:编辑:以上适用于版本 0.12.1 和 0.13
As mentioned above, you need the
autoreload
extension. If you want it to automatically start every time you launchipython
, you need to add it to theipython_config.py
startup file:It may be necessary to generate one first:
Then include these lines in
~/.ipython/profile_default/ipython_config.py
:As well as an optional warning in case you need to take advantage of compiled Python code in
.pyc
files:edit: the above works with version 0.12.1 and 0.13
已修订 - 请参阅下面 Andrew_1510 的答案,因为 IPython 已更新。
...
从满是灰尘的错误报告中弄清楚如何到达那里有点困难,但是:
它现在随 IPython 一起提供了!
...然后每次您调用
your_mod.dwim()
时,它都会选择最新版本。REVISED - please see Andrew_1510's answer below, as IPython has been updated.
...
It was a bit hard figure out how to get there from a dusty bug report, but:
It ships with IPython now!
... then every time you call
your_mod.dwim()
, it'll pick up the latest version.如果您使用如下行将文件 ipython_config.py 添加到 ~/.ipython/profile_default 目录中,则自动重新加载功能将在 IPython 启动时加载(在 2.0.0 上测试):
If you add file ipython_config.py into the ~/.ipython/profile_default directory with lines like below, then the autoreload functionality will be loaded on IPython startup (tested on 2.0.0):
您可以使用:
You can use:
有一个扩展,但我还没有使用经验:
http://ipython.scipy.org/ipython/ipython/attachment/ticket/154/ipy_autoreload.py
There is an extension for that, but I have no usage experience yet:
http://ipython.scipy.org/ipython/ipython/attachment/ticket/154/ipy_autoreload.py