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:
For Python 3.4+:
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:
# you can check out the the documentation for the rest of the autoreaload modes
# by apending a question mark to %autoreload, like this:
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.
Have something to say? Join the discussion below 👇
Want to explore instead? Fly with the time capsule 🛸
You may also find these interesting
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.
Two programming language giants appear on stage for a massive rap battle. Who will win?
Go is an 80%-language. That’s why.