Live-reloading of Python Modules in the Python REPL / IPython / Jupyter Console
Often, I would work on a Python module and add changes to it live, which I would then want to test immediately. Piece of cake! Add the changes and execute the script again. Well, that’s certainly a way, but as modules get bigger, they tend to import other modules, or do some preliminary setup work.
With time, I have developed this practice of opening up a Python REPL (though I recommend IPython or the Jupyter Console for that), importing my in-progress module and singling out separate functions I would like to test. The problem is, when I do changes to the Python code (like, add a new function), they are not immediately usable, because neither the Python REPL, nor IPython / Jupyter would auto-reload them.
Give me the code #
Yes, we are coming to that
General Way #
Python 3 supports inline reloading of modules using a function called, well clever enough, reload
. It used to be a built-in function in Python 2, but this is no longer the case. If you are using Python 3.2+, you should import it extra:
For Python 3.2 and 3.3:
import importlib
importlib.reload(some_module)
For Python 3.4+:
import imp
imp.reload(some_module)
IPython/Jupyter Magic #
Jupyter comes with a set of extensions only applicable to the Jupyter/iPython sessions, called magics. One of these “magics” is the ability to load custom extensions, one of which allows auto-reloading of modules. To enable this, you should add the following two commands, before any imports:
%load_ext autoreload
%autoreload 2
# you can check out the the documentation for the rest of the autoreaload modes
# by apending a question mark to %autoreload, like this:
# %autoreload?
This way, as soon as you hit Save
 in your code editor, you should be able to re-run a Jupyter cell or an iPython line again, and if it is calling your module, it should automatically call the latest version.
Links #
Have something to say? Join the discussion below 👇
Want to explore instead? Fly with the time capsule 🛸
You may also find these interesting
Use Dotenv Files When Developing Your Python Apps
Developing Python apps can be simplified with the use of .env files. These files help store sensitive information, such as API keys and passwords, in a secure and convenient way.
Epic Rap Battles of Programming: Python vs. OCaml
Two programming language giants appear on stage for a massive rap battle. Who will win?
Python is Easy. Go is Simple. Simple != Easy.
Python and Go have distinct qualities that can complement each other.
Any Code Style You Like, So Long As It’s Black
Write clear and consistently indented Python code with zero effort.