gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:26:27 25 4
gpt4 key购买 nike

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

关于python - 在 IPython 中自动重新加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com