The Python Oracle

Reloading submodules in IPython

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC F Haydns String Quartet No 53 in D

--

Chapters
00:00 Question
01:16 Accepted answer (Score 791)
02:18 Answer 2 (Score 76)
03:04 Answer 3 (Score 39)
03:31 Answer 4 (Score 24)
03:56 Thank you

--

Full question
https://stackoverflow.com/questions/5364...

Accepted answer links:
[automatic reloading]: http://ipython.readthedocs.io/en/stable/...

Answer 2 links:
[importlib]: https://docs.python.org/3/library/import...
[importlib.reload()]: https://docs.python.org/3/library/import...
[imp]: https://docs.python.org/3/library/imp.ht...
[importlib.reload()]: https://docs.python.org/3/library/import...

Answer 3 links:
[pv.]: https://stackoverflow.com/users/108184/p...

Answer 4 links:
[import code from one notebook to another]: https://stackoverflow.com/a/16966830/304...
[[1]]: https://stackoverflow.com/a/6420389/3042...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ipython

#avk47



ACCEPTED ANSWER

Score 877


IPython comes with some automatic reloading magic:

%load_ext autoreload
%autoreload 2

It will reload all changed modules every time before executing a new line. The way this works is slightly different than dreload. Some caveats apply, type %autoreload? to see what can go wrong.


If you want to always enable this settings, modify your IPython configuration file ~/.ipython/profile_default/ipython_config.py[1] and appending:

c.InteractiveShellApp.extensions = ['autoreload']     
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

Credit to @Kos via a comment below.

[1] If you don't have the file ~/.ipython/profile_default/ipython_config.py, you need to call ipython profile create first. Or the file may be located at $IPYTHONDIR.




ANSWER 2

Score 43


In IPython 0.12 (and possibly earlier), you can use this:

%load_ext autoreload
%autoreload 2

This is essentially the same as the answer by pv., except that the extension has been renamed and is now loaded using %load_ext.




ANSWER 3

Score 26


For some reason, neither %autoreload, nor dreload seem to work for the situation when you import code from one notebook to another. Only plain Python reload works:

reload(module)

Based on [1].




ANSWER 4

Score 21


IPython offers dreload() to recursively reload all submodules. Personally, I prefer to use the %run() magic command (though it does not perform a deep reload, as pointed out by John Salvatier in the comments).