2009-12-15 35 views
104

有沒有辦法讓IPython自動重裝所有更改的代碼?在shell中執行每行之前,或者在特定請求時執行失敗。我正在使用IPython和SciPy進行大量的探索性編程,每當我改變它時必須手動重新加載每個模塊是一件非常痛苦的事情。IPython中的模塊自動重裝

+0

這裏它是作爲擴展http://projects.scipy實現的。org/ipython/ipython/ticket/154 – 2009-12-15 15:09:41

+0

你可能會考慮改變接受的答案。 – Peque 2015-09-25 08:55:45

+0

我認爲接受回答這個問題的第一個答案通常是公平的,但在幾年後改變它似乎有點意思! – 2015-09-28 09:58:53

回答

53

修訂 - 請參見下面Andrew_1510的answer,如IPython中已經更新。

...

這是一個有點難以搞清楚如何從一個塵土飛揚的bug報告那裏,但:

現在附帶IPython的!

import ipy_autoreload 
%autoreload 2 
%aimport your_mod 

# %autoreload? for help 

...然後每次您撥打your_mod.dwim()時,它都會提取最新版本。

+4

如果不那麼直接? '%run sometest.py'包含'import themod'。在編輯'themod.py'之後,我想只運行'%run sometest.py',但它沒有選擇變化。 – Jed 2011-05-22 08:20:46

+2

我認爲ipython 0.11取消了這個功能。還是隻是重新命名/隱藏在某個地方? – SirVer 2011-08-01 08:51:30

+0

SirVer,你是對的。嘆。 很明顯,它在'檢疫'包中: http://www.archlinux.org/packages/community/any/ipython/files/ – 2011-08-19 05:51:24

165

對於IPython的3.1版本,4.x版,和5.x

In [1]: %load_ext autoreload 

    In [2]: %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 
65

正如上面提到的,你需要的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') 

除了在情況下,可選的警告,你需要利用編譯Python代碼在.pyc文件:

c.InteractiveShellApp.exec_lines.append('print "Warning: disable autoreload in ipython_config.py to improve performance." ') 

編輯:上述工程與0.12.1版本和0.13

+1

這實際上很棒。我想知道爲什麼沒有人發佈解決方案來保存它。這是否也適用於較老版本的IPython?我一直在使用0.12+。我記得ipython存儲自定義的方式發生了很大變化。 – 2012-12-27 23:55:30

+0

我使用0.12.1,並且還沒有嘗試過0.13,所以我不知道它是否可以與0.13+ – 2013-01-02 18:12:40

+5

一起使用這是一個很好的方法,但我認爲你所需要做的就是填充擴展它應該在第27行左右:c.InteractiveShellApp.extensions = ['autoreload']' – dvreed77 2013-05-16 16:15:15

2

您可以使用:

import ipy_autoreload 
    %autoreload 2 
    %aimport your_mod 
8

如果添加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')