Autoreload a module#

You can work on a new python module and test it in a notebook environment. But when the code of your module change, you must reload the module in the notebook environment again.

Simple solution: Use the autoreload to make sure the latest version of the module is used.

Reload modules in a notebook by:

%load_ext autoreload
%autoreload 2

The autoreloading module is not enabled by default. So you have to load it as an extension.

And each time you execute some code, IPython will reimport all the modules to make sure that you are using the latest possible versions.

There are 3 configuration options that you can set:

  • %autoreload 0 : Disables the auto-reloading. This is the default setting.

  • %autoreload 1 :Will only auto-reload modules that were imported using the %aimport function (e.g %aimport my_module). It’s a good option if you want to specifically auto-reload only a selected module.

  • %autoreload 2 : auto-reload all the modules. Great way to make writing and testing your modules much easier.