The only Jupyter Notebooks extension you truly need

Now you don’t need to restart the kernel after editing the code in your custom imports.

By Olga Chernytska, Senior Machine Learning Engineer


When working in Jupyter Notebooks it is convenient to store your functions and classes in separate .py files and just import them. This makes your notebooks readable, and you can easily reuse the same functions and classes in several notebooks.

Assume, you started your notebook, imported custom functions from .py files, loaded large datasets, and did some preprocessing using your custom functions. And it turns out that one of the functions for preprocessing is incorrect, so you fix the code in .py files, then run the cell with preprocessing once again – and… nothing changes, the same error is there. Jupyter Notebook does not see an updated version of the function.

Unfortunately, the only thing you can do here is to restart the kernel, losing all the datasets already loaded to memory.

Unless you are using autoreload.


How it works

According to documentation, autoreload “reloads modules automatically before executing user code” [1].

This means that now you simply: correct function in .py files, save changes, go back to the notebook and run cell with preprocessing. All functions in notebook imports are reloaded to their latest versions, code in the cell is executed – and you see how the fixed function works.




To make this magic come true, put this code snippet in the first cell of your notebook, and run it first after starting the kernel:

%load_ext autoreload
%autoreload 2

You need to run it only once when the kernel is started. And autoreload will work till you stop the kernel.



No installation is needed, it should already be there. Autoreload is installed automatically when you install Jupyter Notebook or Jupyter Lab.



Autoreload allows you to reload only specific modules, or exclude some of the modules from reloading. If you need this feature, please, check the official documentation [1].



[1] IPython Documentation

Bio: Olga Chernytska is a Senior Machine Learning Engineer in a large Eastern European outsourcing company; was involved in various data science projects for top US, European and Asian companies; main specialization and interest is Deep Computer Vision.

Original. Reposted with permission.