Skip to content
Home » Module Reloading

Module Reloading


Module Reloading in Python

When a module is imported using the import statement, Python loads it only once during a program’s execution—even if you import it multiple times.

This means:

✔ The module is loaded into memory only on the first import
✔ Subsequent imports use the cached version
✔ If you modify the module’s code after importing, Python will not automatically reload it

To load the updated version, you must reload the module manually.

This process is called module reloading.


Why Do We Need Module Reloading?

Module reloading is useful when:

✔ You are modifying a module frequently (during development)
✔ You want Python to use the updated code without restarting the program
✔ Using interactive shell / Jupyter Notebook
✔ Working with dynamic configurations

Without reloading, Python continues to use the old version stored in memory.


How to Reload a Module?

Python provides a function for this:

importlib.reload(module_name)

But first, you must import the module and the importlib library.


Example of Module Reloading

Assume we have a module named mymodule.py

Initial code:

# mymodule.py
def greet():
    print("Hello, Python!")

Import it:

import mymodule
mymodule.greet()   # Output: Hello, Python!

Now modify mymodule.py:

def greet():
    print("Hello, Welcome to Programming!")

If you run again:

mymodule.greet()

❌ Python still prints:

Hello, Python!

Because it uses the cached old version.


Reloading the Module

import importlib
importlib.reload(mymodule)

Now call the function again:

mymodule.greet()

✔ Output:

Hello, Welcome to Programming!

Syntax for Reloading a Module

import importlib
importlib.reload(module_name)

Important Points About Reloading

reload() loads the latest version of the module
✔ Works only on modules already imported
✔ Useful during development
✔ Only reloads the module object—not objects already imported using from ... import


Reload Does Not Work for:

If you import like this:

from mymodule import greet

Then:

importlib.reload(mymodule)
greet()  # ❌ still old version

Reason:

  • greet is a copy of the function, not a reference
  • Reloading module does not update this copy

To avoid this problem:
👉 Always use import module_name during development.


Practical Example

import mymodule
import importlib

print("First call:")
mymodule.greet()

# After modifying the file
print("Reloading module...")
importlib.reload(mymodule)

print("Second call:")
mymodule.greet()

Exam-Ready Short Answer

Module reloading is the process of re-importing a module so that any changes made to the module file during program execution are reflected. Python loads a module only once, so we use importlib.reload(module) to load the updated version. Reloading is useful during testing, debugging, and development.