python - 在 IPython 中自动重新加载模块

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

最佳答案

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

https://stackoverflow.com/questions/1907993/

相关文章:

unix - 如何交错来自两个文本文件的行

c - 如何使用 GDB 修改内存内容?

python - 在 Python 中 Ping 服务器

python - 为什么这个 Python 代码段中允许使用分号?

python - 在批处理文件中休眠

linux - 如何 RSYNC 单个文件?

python - Anaconda 与 miniconda

linux - 如何从文本文件中删除非 UTF-8 字符

python - 如何在 Windows 上使用 Python 3 连接到 MySQL?

linux - Shell - 将变量内容写入文件