Is there a way to have IPython automatically reload all changed code? Either before each line is executed in the shell or failing that when it is specifically requested to. I'm doing a lot of exploratory programming using IPython and SciPy and it's quite a pain to have to manually reload each module whenever I change it.
This question is related to
python
jupyter-notebook
ipython
If you add file ipython_config.py into the ~/.ipython/profile_default directory with lines like below, then the autoreload functionality will be loaded on IPython startup (tested on 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')
There is an extension for that, but I have no usage experience yet:
http://ipython.scipy.org/ipython/ipython/attachment/ticket/154/ipy_autoreload.py
As mentioned above, you need the autoreload
extension. If you want it to automatically start every time you launch ipython
, you need to add it to the ipython_config.py
startup file:
It may be necessary to generate one first:
ipython profile create
Then include these lines in ~/.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')
As well as an optional warning in case you need to take advantage of compiled Python code in .pyc
files:
c.InteractiveShellApp.exec_lines.append('print "Warning: disable autoreload in ipython_config.py to improve performance." ')
edit: the above works with version 0.12.1 and 0.13
You can use:
import ipy_autoreload
%autoreload 2
%aimport your_mod
REVISED - please see Andrew_1510's answer below, as IPython has been updated.
...
It was a bit hard figure out how to get there from a dusty bug report, but:
It ships with IPython now!
import ipy_autoreload
%autoreload 2
%aimport your_mod
# %autoreload? for help
... then every time you call your_mod.dwim()
, it'll pick up the latest version.
Source: Stackoverflow.com