Factor Olga Chernytska, Senior Machine Learning Engineer

When working with Jupyter Notebooks, it is convenient to save functions and categories as separate .py files and only import them. This makes your notebooks readable, and you can easily access the same functions and categories across multiple notebooks.

Suppose you started a notebook, imported custom functions from .py files, loaded large data sets, and did some preprocessing using custom functions. And it turns out that one of the preprocessing operations is invalid, so you fix the code in the .py files, then run the cell again with preprocessing – and … nothing changes, the same error is there. Jupyter Notebook does not see an updated version of the feature.

Unfortunately, the only thing you can do here is reboot the kernel by losing all the data sets already loaded into memory.

If you do not use autoreload.

How does it work

According to the documents autoreload “Automatically load modules before executing user code” [1].

This means that now simply: fix the functions of the .py files, save the changes, go back to the notebook, and perform cell preprocessing. All the import functions for laptops are downloaded to their latest versions, the cell code is executed – and you can see how the fixed function works.

Picture

Use

To make this magic happen, put this snippet of code in the first cell of the notebook and run it first after booting the kernel:

%load_ext autoreload
%autoreload 2

You only need to run it once when the kernel is booted. And autoreload works until you stop the kernel.

Installation

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

More

Autoreload you can download only certain modules or close some modules for reloading. If you need this feature, check the official documents [1].

References

[1] IPython documentation

Bio: Olga Chernytska is a senior machine learning engineer in a large Eastern European outsourcing company; was involved in several data science projects for top companies in the US, Europe and Asia; main specialization and interest is Deep Computer Vision.

Original. Re-posted with permission.

Related to:

LEAVE A REPLY

Please enter your comment!
Please enter your name here