有没有办法让IPython自动重载所有更改的代码?要么在shell中执行每一行之前执行,要么在被特别要求执行时执行失败。我使用IPython和SciPy进行了大量探索性编程,每当我更改每个模块时,都必须手动重新加载它,这非常痛苦。
当前回答
修订-请参阅下面Andrew_1510的答案,因为IPython已经更新。
...
从满是灰尘的bug报告中找到答案有点困难,但是:
它现在与IPython一起发布!
import ipy_autoreload
%autoreload 2
%aimport your_mod
# %autoreload? for help
... 然后,每次调用your_mod.dwim()时,它都会获取最新版本。
其他回答
有一个扩展,但我还没有使用经验:
http://ipython.scipy.org/ipython/ipython/attachment/ticket/154/ipy_autoreload.py
修订-请参阅下面Andrew_1510的答案,因为IPython已经更新。
...
从满是灰尘的bug报告中找到答案有点困难,但是:
它现在与IPython一起发布!
import ipy_autoreload
%autoreload 2
%aimport your_mod
# %autoreload? for help
... 然后,每次调用your_mod.dwim()时,它都会获取最新版本。
如果将ipython_config.py文件添加到~/. conf文件中。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')
对于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
你可以使用:
import ipy_autoreload
%autoreload 2
%aimport your_mod
推荐文章
- 使用python创建一个简单的XML文件
- APT命令行界面式的yes/no输入?
- 如何打印出状态栏和百分比?
- 在Python中获取大文件的MD5哈希值
- 在Python格式字符串中%s是什么意思?
- 如何循环通过所有但最后一项的列表?
- python用什么方法避免默认参数为空列表?
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?
- 打印测试执行时间并使用py.test锁定缓慢的测试
- 插入一行到熊猫数据框架
- 要列出Pandas DataFrame列
- 在Django模型中存储电话号码的最佳方法是什么?
- 从导入的模块中模拟函数