This post is going to note down how to let ipython automatically reload a module after changing the module and how to run magic function from python start-up scripts.

ipython will not reload any modules if you have changed some module under given that ipython want to keep the interactive session so all your data during the session is going to be kept. To overcome this situation while you are writing some module or library code, the autoload magic function could be used.

%load_ext autoreload

%autoreload 2
%autoreload
Reload all modules (except those excluded by %aimport) automatically now.

%autoreload 0
Disable automatic reloading.

%autoreload 1
Reload all modules imported with %aimport every time before executing the Python code typed.

%autoreload 2
Reload all modules (except those excluded by %aimport) every time before executing the Python code typed.

%aimport
List modules which are to be automatically imported or not to be imported.

%aimport foo
Import module ‘foo’ and mark it to be autoreloaded for %autoreload 1

%aimport -foo
Mark module ‘foo’ to not be autoreloaded.

For more details, refer to ipython’s documentation.

To avoid typing those magic function again and again, they could be put in the ipython startup script(Name it with .py suffix under .ipython/profile_default/startup. All python scripts under that folder will be loaded according to lexical order), which looks like the following:

from IPython import get_ipython
ipython = get_ipython()

ipython.magic("pylab")
ipython.magic("load_ext autoreload")
ipython.magic("autoreload 2")