Reloading modules in Python
Last Updated : 21 Jun, 2025
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.
Python 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.
Python 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().
Python 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:
Python 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.
Similar Reads
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
C Extension Module using Python Writing a simple C extension module directly using Pythonâs extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file. Code #1 : C #include <math.h> extern int gcd
4 min read
Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read