Reloading submodules in IPython
Module named importlib
allow to access to import internals. Especially, it provide function importlib.reload()
:
import importlib
importlib.reload(my_module)
In contrary of %autoreload
, importlib.reload()
also reset global variables set in module. In most cases, it is what you want.
importlib
is only available since Python 3.1. For older version, you have to use module imp
.
I suggest to read documentation of importlib.reload()
to get the list of all caveats of this function (recursive reload, cases where definitions of old objects remain, etc...).
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
.
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].
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
.