Reloading modules in Python
We are given a case where we want to update and test a Python module without restarting the interpreter. This is especially helpful during development when you're modifying module files externally and want those changes to reflect immediately. Python allows us to reload a previously imported module using the reload() function. For example, if you've edited a utility file utils.py while your script is running, reloading lets you re-import the updated content without restarting the Python shell.
When Should You Reload a Module?
- During debugging or live testing
- When using interactive interpreters like IPython or Jupyter
- While developing plugins or modular systems
- To reflect changes made in external .py files without restarting the session
Reloading Modules in Python 2.x
In Python 2.x, you can directly use the built-in reload() function to reload a module that was previously imported.
import mymodule
# Modify 'mymodule.py' externally here...
reload(mymodule)
Explanation:
- mymodule is first imported using import.
- After making external changes to mymodule.py, calling reload(mymodule) reloads the updated version without restarting the interpreter.
- No need to re-import — reload() works on an already imported module object.
Reloading Modules in Python 3.0 to 3.3
In Python versions 3.0 to 3.3, the built-in reload() function was removed. Instead, you need to import it from the imp module.
import mymodule
import imp
# Modify 'mymodule.py' externally...
imp.reload(mymodule)
Explanation:
- First, mymodule is imported as usual.
- The imp module provides the reload() function during this version range.
- After editing mymodule.py, imp.reload(mymodule) reloads the module with updated changes.
Note: The imp module is deprecated as of Python 3.4 and is replaced by importlib.
Reloading Modules in Python 3.4 and Above
Starting from Python 3.4, the recommended way to reload a module is by using importlib.reload().
import mymodule
import importlib
# Modify 'mymodule.py' externally...
importlib.reload(mymodule)
Explanation:
- mymodule is first imported normally.
- Then, importlib.reload(mymodule) reloads the updated module.
- This is the current and official way to reload modules in Python 3.4+.
Note: Use this method in all modern Python scripts and notebooks when testing module changes.
Can You Unload a Module in Python?
Currently, Python does not provide a built-in way to fully unload a module once it's imported. Once a module is loaded into memory, it stays available in sys.modules until the program ends. Although you can delete a module from sys.modules, this doesn't guarantee its complete removal from memory or that its references are gone:
import mymodule
import sys
del sys.modules['mymodule']
This will only remove the module from the sys.modules cache. If any variable or object is still referencing the module, it won’t be fully unloaded.
Why Unloading Isn’t Supported?
- Python manages modules globally in memory.
- Unloading dynamically could lead to inconsistent behavior if parts of your code still hold references to the old module.