295

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.

Nikita
  • 333
  • 3
  • 8
Thomas Parslow
  • 5,712
  • 4
  • 26
  • 33

6 Answers6

515

For IPython version 3.1, 4.x, and 5.x

%load_ext autoreload
%autoreload 2

Then your module will be auto-reloaded by default. This is the doc:

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.

There is a trick: when you forget all of the above when using ipython, just try:

import autoreload
?autoreload
# Then you get all the above
rfho_bdss
  • 170
  • 1
  • 18
Andrew_1510
  • 12,258
  • 9
  • 51
  • 52
  • 2
    Is there a way to do this in `ipdb`? Say, I am in ipd, and I notice a line didnt work. So I changed the line, and want to reload the file. Will this work? – alpha_989 Mar 30 '18 at 16:49
  • 1
    An improvement to the first line first checks to see if autoreload has already been loaded: `if 'autoreload' not in get_ipython().extension_manager.loaded:\n %load_ext autoreload\n %autoreload 2`. This will get rid of the following error that appears when executing the command again: `The autoreload extension is already loaded. To reload it, use:\n %reload_ext autoreload`. – user3897315 Mar 25 '21 at 22:29
  • 3
    What does 2 in `%autoreload 2` mean? – Coding Tumbleweed Jun 18 '21 at 12:51
  • 2
    the `2` in `%autoreload 2` means `Reload all modules (except those excluded by %aimport) every time before executing the Python code typed.` https://ipython.org/ipython-doc/3/config/extensions/autoreload.html – eth4io Jul 10 '21 at 11:41
  • If you want this set automatically on every session in PyCharm, you can add it to the startup script in Settings → Buld, Execution, Deployment → Console → Python Console → Startup Script. You must use a Conda interpreter to get it to work in PyCharm. – Joakim Apr 19 '22 at 10:00
  • 2
    I'm coming here every single time I have to do this. They are two lines that I cannot / don't want to remember – Juan Luis Ruiz-tagle Sep 27 '22 at 08:40
  • 2
    Same here @JuanLuisRuiz-tagle, about 5 years googling and coming to this thread haha – Fernando Wittmann Nov 03 '22 at 19:22
120

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

Josh
  • 12,896
  • 4
  • 48
  • 49
kara deniz
  • 2,396
  • 1
  • 19
  • 15
  • 1
    This is actually great. I was wondering why no one else was posting solutions to preserve it. Does this work with older versions of IPython as well? I've been using 0.12+. I recall that the way ipython stores customizations changed significantly. – Ehtesh Choudhury Dec 27 '12 at 23:55
  • I'm using 0.12.1, and haven't yet tried 0.13, so I don't know whether it will work with 0.13+ – kara deniz Jan 02 '13 at 18:12
  • 6
    This is a good approach, but I think all you need to do is fill in the extenstions which should be around line 27: `c.InteractiveShellApp.extensions = ['autoreload']` – dvreed77 May 16 '13 at 16:15
  • 11
    use `c.InteractiveShellApp.extensions = ['autoreload']`, and `c.InteractiveShellApp.exec_lines = ['%autoreload 2']`. I am not sure but in the default profile of version 0.13 under Ubuntu 13.04 I found a 'startup' folder that contains a script '50_autoreload.ipy' to activate autoreload. Maybe nothing is required at all – spinxz May 28 '13 at 17:41
  • 1
    I have to find this answer on any new install, this is the only sane config for development in iPython. – dashesy Sep 14 '13 at 19:34
  • This file doesnt seem to be there by default so you might have to create it as he said. Also before the code add ... c = get_config() #Should be first line – Leon May 02 '14 at 18:20
  • Is this still needed for ipython 1.2.1.? And 3.0.0? – Frederick Nord Jan 18 '15 at 11:42
  • Since `--pylab` is not longer an option I use `c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']` – dashesy May 18 '16 at 19:35
69

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.

Community
  • 1
  • 1
Mike McCabe
  • 1,015
  • 9
  • 9
  • 4
    What if it is less direct? `%run sometest.py` contains `import themod`. After editing `themod.py`, I'd like to just `%run sometest.py`, but it doesn't pick up the changes. – Jed May 22 '11 at 08:20
  • 2
    I think ipython 0.11 did away with this feature. Or is it just renamed/hidden someplace? – SirVer Aug 01 '11 at 08:51
  • 1
    SirVer, you're right. Sigh. Evidently, it's in the 'quarantine' package: http://www.archlinux.org/packages/community/any/ipython/files/ – Mike McCabe Aug 19 '11 at 05:51
  • Explanation [here](http://ipython.org/ipython-doc/stable/whatsnew/version0.11.html#quarantine) - with an invitation to port to 0.11 :) 'from IPython.quarantine import ipy_autoreload' succeeds, and creates an %autoreload command... but in my initial tests, it doesn't seem to work. – Mike McCabe Aug 19 '11 at 05:58
  • Looks like it's back in as of 9/30/2011 - so maybe we'll see it in an upcoming release. https://github.com/ipython/ipython/pull/746 – Mike McCabe Nov 15 '11 at 04:35
  • 1
    What if I wanted to do "from moduleX import blah"? – exfizik Oct 03 '14 at 20:42
  • @exfizik, IIUC, I think you need `%aimport moduleX` and then (on a subsequent line in the notebook) `from moduleX import blah` – edesz Aug 27 '19 at 17:20
15

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')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lowtech
  • 2,492
  • 2
  • 22
  • 31
2

You can use:

  import ipy_autoreload
  %autoreload 2 
  %aimport your_mod
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
dongweiming
  • 811
  • 1
  • 8
  • 8
0

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

miku
  • 181,842
  • 47
  • 306
  • 310