Reload a module and its submodules in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Reload a module and its submodules in Jupyter Notebook

To reload a module and its submodules in Jupyter Notebook:

  1. Use the %load_ext autoreload magic command to enable automatic reloading.
  2. Use %autoreload 2 to specify that all modules should be reloaded before executing the Python code.
main.py
%load_ext autoreload %autoreload 2

reload module and its submodules

Here is a complete example of a complete workflow.

main.py
%load_ext autoreload %autoreload 2 # Import your module from my_module import my_function my_function() # 100 # Open your IDE and update my_function to return 200 my_function() # 200

reload module in jupyter complete example

The autoreload magic command reloads modules automatically before executing your code.

All changed modules are reloaded before a new line is executed.

The module in the example above was reloaded and the function that was imported from the module was also updated.

The %autoreload 2 line reloads all modules every time before executing your code.

You can print additional information about the %autoreload magic command with %autoreload?.

main.py
%autoreload?

print details of autoreload

When you use the %autoreload magic command:

  1. Functions and classes that were imported via from foo import bar are upgraded to new versions when foo is reloaded.
  2. Methods and properties of classes are upgraded on reload, so calling A.bar() on an object A created before the reload causes the new code for bar() to be executed.

Some things to look out for when using this approach:

  1. Functions that are removed from a module before it is reloaded are not upgraded.
  2. C extension modules cannot be auto-reloaded.
  3. Changing an @property in a class to a method or a method to a class variable can cause issues.

# Using importlib.reload() to reload a module in Jupyter Notebook

You can also use the importlib.reload() method to reload a module in Jupyter Notebook.

main.py
import my_module import importlib importlib.reload(my_module)

using importlib reload to reload module in jupyter notebook

The importlib.reload() method reloads a previously imported module.

The only argument the method takes is the module object you want to reload.

Note that the module must have been imported before passing it to the importlib.reload() method.

The importlib.reload() method also resets global variables that you've set in the module whereas the %autoreload magic command doesn't.

Using the importlib.reload() method is useful if you have edited the module's source code using an external code editor and want to try the new version without having to restart Jupyter Notebook or the Python interpreter.

The importlib.reload() method returns the module object.

main.py
import os import importlib module_obj = importlib.reload(os) # 👇️ <module 'os' from '/usr/lib/python3.10/os.py'> print(module_obj)

The module object might be different if re-importing causes a different object to be placed in sys.modules.

When the importlib.reload() method is called:

  • The module-level code is re-executed and a new set of objects are defined and bound to names in the module's dictionary.
  • The old objects are only reclaimed after their reference count drops to 0.
  • The names in the module namespace are updated to point to any new or changed objects.
  • If a module creates instances of a class, reloading the module that defines the class doesn't affect method definitions of the instances - they continue to use the old class definition.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.