Of course! In Python, "reloading" a module means forcing Python to re-read the module's source code file and re-execute it, updating the module object in memory. This is extremely useful for interactive development and testing without having to restart the Python interpreter.

Here’s a complete guide, from the simple answer to best practices and important caveats.
The Quick and Simple Answer
The standard way to reload a module is by using the importlib.reload() function.
- Import the module:
import my_module - Make changes to
my_module.pyin your editor. - Reload the module:
importlib.reload(my_module)
Example:
Let's say you have a file named calculator.py:
# calculator.py
def add(a, b):
print("Using the OLD add function.")
return a + b
VERSION = "1.0"
Now, in your Python interpreter or script:
# Step 1: Import the module for the first time
>>> import calculator
>>> calculator.add(2, 3)
Using the OLD add function.
5
# Step 2: Change the source code in calculator.py
# Let's change it to this:
# def add(a, b):
# print("Using the NEW and IMPROVED add function.")
# return a + b
# VERSION = "2.0"
# Step 3: Reload the module
>>> import importlib
>>> importlib.reload(calculator)
<module 'calculator' from '/path/to/your/calculator.py'>
# Step 4: Use the module again. You'll see the change!
>>> calculator.add(2, 3)
Using the NEW and IMPROVED add function.
5
How It Works: A Deeper Dive
When you call importlib.reload(module), Python performs the following steps:

- Re-executes the Code: It re-runs the entire source code of the module from top to bottom.
- Updates the Namespace: The module's global namespace (the dictionary you see with
module.__dict__) is updated with new or changed names. - Handles Names in Other Modules: This is the tricky part. If another module, say
main.py, had already donefrom calculator import add, the nameaddinmain.py's namespace is not updated. It still points to the old function object. Reloading only changes thecalculatormodule object itself.
Illustrating the "Stale Reference" Problem:
# main_script.py from calculator import add # This imports the OLD 'add' import importlib print(f"Calling add from main_script before reload:") add(10, 5) # Output: "Using the OLD add function." # Now, let's assume we've re-saved calculator.py with the new 'add' function. # We reload the calculator module. importlib.reload(calculator) print(f"\nCalling add from main_script after reload:") add(10, 5) # Output: STILL "Using the OLD add function."
As you can see, main_script's local name add is a "stale reference" to the old function. Only calls that go through the module object itself (calculator.add()) will use the new code.
The Best Practice: The IPython Magic Command %autoreload
If you are doing interactive development in a Jupyter Notebook or an IPython shell, manually typing importlib.reload() is tedious. The best solution is to use the %autoreload magic command.
This automatically reloads modules before executing code.
How to use it:
- Start your IPython or Jupyter session.
- Run the magic command once at the beginning.
%load_ext autoreload %autoreload 2
%autoreload 2means "reload all modules (except those explicitly excluded) before every execution." This is the most common setting.%autoreload 1means "reload modules only when their code has been executed since the last reload."
Example with %autoreload 2:

# In a Jupyter cell %load_ext autoreload %autoreload 2 # Now, just import your module as usual import calculator # Call a function calculator.add(2, 3) # Output: "Using the OLD add function." # Now, go edit calculator.py in your editor and save it. # No need to run anything else! # Run the cell again. It will automatically reload! calculator.add(2, 3) # Output: "Using the NEW and IMPROVED add function."
This is the most convenient and recommended method for interactive development.
Important Caveats and Limitations
Reloading is powerful but not magic. Be aware of these issues:
-
Classes and Instances:
- If you have a class
MyClassin your module and you've already created an instanceobj = MyClass(), reloading the module does not change the behavior ofobj. The instance was created using the old class definition. - New instances created after the reload will use the new class definition.
- If you have a class
-
C-Extensions: Reloading C-extensions (modules written in C, like many in NumPy or built-in modules) is not supported by
importlib.reloadand will raise aTypeError. -
__pycache__Directory: Reloading is based on the timestamp of the source file (.py). If you change the source, Python will recompile it to a new.pycfile in the__pycache__directory. This process is handled automatically. -
from ... import ...: As discussed, this creates stale references. The safest way to import when you plan to reload isimport moduleand always access its contents viamodule.name.
Summary: Which Method to Use?
| Scenario | Recommended Method | Why? |
|---|---|---|
| Interactive Development (Jupyter, IPython) | %autoreload 2 |
It's automatic, convenient, and requires no extra code in your scripts. |
| Scripting / Programming | importlib.reload(module) |
It's explicit, part of the standard library, and works in any Python environment. |
| Production / Long-Running Apps | Avoid reloading. | Restart the process. Reloading can lead to unpredictable state and memory leaks. It's a development tool, not a production feature. |
