有没有办法让 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/