Python
Module Reimport
Dynamic Code
Python Import
Code Modification

how to reimport module to python then code be changed after import

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Python imports a module, it caches that module object in sys.modules. Changing the source file afterward does not automatically update the already imported module in the current process. If you want Python to pick up the new code without restarting the interpreter, the usual tool is importlib.reload.

Why a Second import Usually Does Not Help

A normal import statement checks whether the module is already loaded. If it is, Python reuses the cached module object instead of re-executing the file.

python
1import math
2import math
3
4print("math imported twice, but only loaded once")

That behavior is a feature. It avoids repeated work and keeps module-level state consistent. It also explains why editing a module file after import seems to have no effect.

Reload a Module with importlib.reload

To re-execute the module's code, import the module object itself and pass it to reload.

python
1import importlib
2import mymodule
3
4print(mymodule.greet())
5
6# Edit mymodule.py here, then run:
7importlib.reload(mymodule)
8print(mymodule.greet())

This tells Python to re-read and re-execute the module source. The module object stays in place, but its contents are updated from the file.

The important detail is that reload works on a module object, not a module name string.

A Small Example

Suppose mymodule.py initially contains:

python
def greet():
    return "version one"

You import it in a Python session:

python
1import importlib
2import mymodule
3
4print(mymodule.greet())

Output:

python
version one

Now change the file to:

python
def greet():
    return "version two"

Then reload it:

python
importlib.reload(mymodule)
print(mymodule.greet())

Now the output becomes:

python
version two

That is the normal development workflow in interactive shells and notebooks when a full restart is inconvenient.

from module import name Has a Catch

One of the most common reasons reload appears not to work is this form:

python
from mymodule import greet

In that case, greet is copied into the current namespace. Reloading mymodule does not automatically replace the already imported local name.

python
1import importlib
2import mymodule
3from mymodule import greet
4
5importlib.reload(mymodule)
6print(greet())           # may still refer to the old function object
7print(mymodule.greet())  # refers to the reloaded module version

If you plan to reload often, prefer import mymodule and then call mymodule.greet().

Reloading Has Limits

reload re-executes the module, but it does not magically rebuild everything that depended on the old objects. Existing class instances continue to use the class definitions they were created from. Functions copied elsewhere may still point to old code objects. Any module-level mutable state may also survive in surprising ways depending on how the module reinitializes itself.

That is why reload is useful mainly for interactive development, not as a general production mechanism for live code replacement.

Common Pitfalls

The first pitfall is using import module again and expecting it to refresh the file. It usually will not, because Python consults sys.modules first.

Another mistake is mixing reload with from module import name and then wondering why the imported function or class did not change. Reload updates the module object, not every local alias created earlier.

Developers also often forget that module-level side effects run again on reload. If the module opens files, registers handlers, or mutates global state during import, reloading can duplicate those effects.

Finally, if you are in a notebook environment, remember that reload solves only part of the problem. Old objects created in earlier cells may still be alive. Sometimes a kernel restart is the cleaner and more predictable option.

Summary

  • Python caches imported modules in sys.modules, so a second import usually does not reload changed code.
  • Use importlib.reload(module) when you need to re-execute a module in the current process.
  • Prefer import module over from module import name if you expect to reload frequently.
  • Reloading updates the module object, but not every old reference or instance created before the reload.
  • For complex stateful programs, restarting the interpreter is often safer than repeated reloads.

Course illustration
Course illustration

All Rights Reserved.