Gibt es eine Möglichkeit, IPython alle geänderten Codes automatisch neu laden zu lassen? Entweder bevor jede Zeile in der Shell ausgeführt wird oder andernfalls, wenn dies ausdrücklich angefordert wird. Ich programmiere viel mit IPython und SciPy und es ist ziemlich mühsam, jedes Modul manuell neu zu laden, wenn ich es ändere.
Für IPython Version 3.1, 4.x und 5.x
%load_ext autoreload
%autoreload 2
Dann wird Ihr Modul standardmäßig automatisch neu geladen sein. Dies ist das Dokument:
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.
Es gibt einen Trick: Wenn Sie bei der Verwendung von ipython
alles vergessen von dem oben Gesagten, versuchen Sie einfach:
import autoreload
?autoreload
# Then you get all the above
Wie oben erwähnt, benötigen Sie die Erweiterung autoreload
. Wenn es bei jedem Start von ipython
automatisch gestartet werden soll, müssen Sie es der Startdatei ipython_config.py
Hinzufügen:
Möglicherweise muss zuerst eine generiert werden:
ipython profile create
Fügen Sie dann diese Zeilen in ~/.ipython/profile_default/ipython_config.py
Ein:
c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')
Zusätzlich zu einer optionalen Warnung, falls Sie kompilierten Python Code in .pyc
- Dateien verwenden müssen:
c.InteractiveShellApp.exec_lines.append('print "Warning: disable autoreload in ipython_config.py to improve performance." ')
edit: das obige funktioniert mit version 0.12.1 und 0.13
ÜBERARBEITET - siehe Andrew_1510s Antwort unten, da IPython aktualisiert wurde.
...
Es war ein bisschen schwierig, anhand eines staubigen Fehlerberichts herauszufinden, wie man dorthin kommt, aber:
Es wird jetzt mit IPython ausgeliefert!
import ipy_autoreload
%autoreload 2
%aimport your_mod
# %autoreload? for help
... und jedes Mal, wenn Sie your_mod.dwim()
aufrufen, wird die neueste Version abgerufen.
wenn Sie ipython_config.py in das Verzeichnis ~/.ipython/profile_default mit den folgenden Zeilen einfügen, wird die Funktion zum automatischen Laden beim Starten von ipython geladen (getestet unter 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')
Sie können verwenden:
import ipy_autoreload
%autoreload 2
%aimport your_mod
Es gibt eine Erweiterung dafür, aber ich habe noch keine Nutzungserfahrung:
http://ipython.scipy.org/ipython/ipython/attachment/ticket/154/ipy_autoreload.py