Automagically Reload Imports In iPython!

When using iPython, users can import required modules to test them. After importing them, if they get modified either by user or some other process, users have to reload it for futher usage.

Depending on the Python version, appropriate reload function can reload modules.

# Python 2.x
In [15]: imp.reload(module)

# Python 3.0–3.3
In [15]: imp.reload(module)

# Python 3.4+
In [15]: importlib.reload(module)

Instead of manually reloading, ipython has autoreload extention which can auto reload modules. For that, load the extention and activate it.

In [15]: %load_ext autoreload

In [16]: %autoreload 2

This can be added to ipython config file so that autoreload gets activated, whenver it starts.

$ ipython profile create

This creates a default config file. Open config file which is present at ~/.ipython/profile_default/ and add these two lines to it.

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

Note that it won't reloads c extensions automatically.

I am Chillar Anand. I daydream a lot and write about the things that interest me here. You can read more about this blog here.

See all articles


Edit this page