IPython 中的模块自动重新加载

发布于 2024-08-15 02:50:26 字数 125 浏览 5 评论 0原文

有没有办法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

许仙没带伞 2024-08-22 02:50:26

对于 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

For IPython version 3.1, 4.x, and 5.x

%load_ext autoreload
%autoreload 2

Then your module will be auto-reloaded by default. This is the doc:

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.

There is a trick: when you forget all of the above when using ipython, just try:

import autoreload
?autoreload
# Then you get all the above
柏拉图鍀咏恒 2024-08-22 02:50:26

如上所述,您需要 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

As mentioned above, you need the autoreload extension. If you want it to automatically start every time you launch ipython, you need to add it to the ipython_config.py startup file:

It may be necessary to generate one first:

ipython profile create

Then include these lines in ~/.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')

As well as an optional warning in case you need to take advantage of compiled Python code in .pyc files:

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

edit: the above works with version 0.12.1 and 0.13

红玫瑰 2024-08-22 02:50:26

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

...

从满是灰尘的错误报告中弄清楚如何到达那里有点困难,但是:

它现在随 IPython 一起提供了!

import ipy_autoreload
%autoreload 2
%aimport your_mod

# %autoreload? for help

...然后每次您调用 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!

import ipy_autoreload
%autoreload 2
%aimport your_mod

# %autoreload? for help

... then every time you call your_mod.dwim(), it'll pick up the latest version.

樱花细雨 2024-08-22 02:50:26

如果您使用如下行将文件 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')

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):

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')
眼泪也成诗 2024-08-22 02:50:26

您可以使用:

  import ipy_autoreload
  %autoreload 2 
  %aimport your_mod

You can use:

  import ipy_autoreload
  %autoreload 2 
  %aimport your_mod
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文