Reload Python module within Jupyter Notebook
All the code in a Jupyter Notebook is the code that runs in the same runtime. When you import the same module multiple times by
import, Python’s package management mechanism will automatically ignore the subsequent imports, and always only use the first import.
There are three ways to reload module:
importlib, But this has to import the that module again in each Cell;
Restart the entire Notebook. But this will cause all the variables in the current runtime to be lost.
These lines of code can only run normally in Jupyter:
# first load the autoreload extension, using the %load_ext magic.
When any cell runs, all modules imported will be reloaded once. This allows you to use the latest code every time.
See the following Usages to learn more.
The following magic commands are provided:
Reload all modules (except those excluded by %aimport) automatically now.
Disable automatic reloading.
Reload all modules imported with %aimport every time before executing the Python code typed.
Reload all modules (except those excluded by %aimport) every time before executing the Python code typed.
List modules which are to be automatically imported or not to be imported.
Import module ‘foo’ and mark it to be autoreloaded for %autoreload 1
Mark module ‘foo’ to not be autoreloaded.
Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur.
%autoreload tries to work around common pitfalls by replacing function code objects and parts of classes previously in the module with new versions. This makes the following things to work:
Functions and classes imported via
from xxx import fooare upgraded to new versions when
Methods and properties of classes are upgraded on reload, so that calling
c.foo()on an object
ccreated before the reload causes the new code for
footo be executed.
Some of the known remaining caveats are:
Replacing code objects does not always succeed: changing a
@propertyin a class to an ordinary method or a method to a member variable can cause problems (but in old objects only).
Functions that are removed (eg. via monkey-patching) from a module before it is reloaded are not upgraded.
C extension modules cannot be reloaded, and so cannot be autoreloaded.
The purpose of the
importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the
__import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.
Two, the components to implement import are exposed in this package, making it easier for users to create their own custom objects (known generically as an importer) to participate in the import process.
This can be use between Python file and Jupyter Notebook, but has to import the that module again in each Cell.