【问题标题】:Autoreload of modules in IPython [duplicate]IPython中模块的自动重新加载[重复]
【发布时间】:2010-12-26 20:26:12
【问题描述】:

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

【问题讨论】:

标签: python jupyter-notebook ipython


【解决方案1】:

【讨论】:

    【解决方案2】:

    已修订 - 请参阅下方 Andrew_1510 的 answer,因为 IPython 已更新。

    ...

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

    它现在随 IPython 一起提供!

    import ipy_autoreload
    %autoreload 2
    %aimport your_mod
    
    # %autoreload? for help
    

    ...那么每次您拨打your_mod.dwim(),它都会获取最新版本。

    【讨论】:

    • 如果不那么直接怎么办? %run sometest.py 包含 import themod。编辑themod.py 后,我想只写%run sometest.py,但它不会接受更改。
    • 我认为 ipython 0.11 取消了这个功能。还是只是重命名/隐藏在某个地方?
    • 先生,您是对的。叹。显然,它在“隔离”包中:archlinux.org/packages/community/any/ipython/files
    • 解释 here - 邀请移植到 0.11 :) 'from IPython.quarantine import ipy_autoreload' 成功,并创建了一个 %autoreload 命令......但在我的初始测试中,它没有似乎没用。
    • 如果我想做“from moduleX import blah”怎么办?
    【解决方案3】:

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

    【讨论】:

    • 有没有办法在ipdb 中做到这一点?说,我在 ipd 中,我注意到一行没有工作。所以我改变了行,并想重新加载文件。这行得通吗?
    • 对第一行的改进首先检查是否已加载自动重载:if 'autoreload' not in get_ipython().extension_manager.loaded:\n %load_ext autoreload\n %autoreload 2。这样就可以摆脱再次执行命令时出现的如下错误:The autoreload extension is already loaded. To reload it, use:\n %reload_ext autoreload
    • %autoreload 2 中的 2 是什么意思?
    • %autoreload 2 中的2 表示Reload all modules (except those excluded by %aimport) every time before executing the Python code typed. ipython.org/ipython-doc/3/config/extensions/autoreload.html
    【解决方案4】:

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

    【讨论】:

    • 这真的很棒。我想知道为什么没有其他人发布解决方案来保护它。这也适用于旧版本的 IPython 吗?我一直在使用 0.12+。我记得 ipython 存储自定义项的方式发生了很大变化。
    • 我用的是0.12.1,0.13还没试过,不知道0.13+能不能用
    • 这是一个很好的方法,但我认为您需要做的就是填写应该在第 27 行附近的扩展名:c.InteractiveShellApp.extensions = ['autoreload']
    • 使用c.InteractiveShellApp.extensions = ['autoreload']c.InteractiveShellApp.exec_lines = ['%autoreload 2']。我不确定,但在 Ubuntu 13.04 下版本 0.13 的默认配置文件中,我发现了一个“启动”文件夹,其中包含一个脚本“50_autoreload.ipy”来激活自动重载。也许什么都不需要
    • 我必须在任何新安装中找到这个答案,这是在 iPython 中开发的唯一合理配置。
    【解决方案5】:

    你可以使用:

      import ipy_autoreload
      %autoreload 2 
      %aimport your_mod
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2016-05-04
        • 2011-07-18
        • 2016-05-20
        • 2018-12-17
        • 1970-01-01
        • 2011-01-08
        • 2017-06-19
        • 2013-05-23
        • 1970-01-01
        相关资源
        最近更新 更多