Python
Unimport
Module Management
Python Programming
Python Tips

How to unimport a Python module that is already imported

Codemia Knowledge Hub ·

Python has no true "unimport" that rewinds a module's execution. The closest you can get is to delete the name from your namespace with del and remove the module from the sys.modules cache, then re-import it if needed. Existing references to the module's objects and any import-time side effects survive, so this is an approximation, not an undo.

Why imports stick: sys.modules

Importing a module executes its code and stores the module object in sys.modules. Later imports reuse the cached object instead of running the file again:

python
1import math
2import sys
3
4print("math" in sys.modules)
5print(sys.modules["math"])

Python assumes imported modules live for the whole interpreter process, which is why there is no built-in unimport().

del vs removing the cache entry

del only removes one reference, the local name:

python
import math

del math

The name math is gone from the current namespace, but the module is still in sys.modules.

Removing the cache entry is a separate step:

python
1import math
2import sys
3
4sys.modules.pop("math", None)

A later import math can then load the module again. References held elsewhere are still not destroyed.

The closest "unimport": do both

python
1import importlib
2import math
3import sys
4
5print(math.sqrt(16))
6
7del math
8sys.modules.pop("math", None)
9
10math = importlib.import_module("math")
11print(math.sqrt(25))

This is useful in experiments, plugin systems, or tests where you want to re-run import-time behavior.

What this does not reset

If another module imported math earlier, or your code saved a function from the target module, those references stay valid after you pop the cache entry. Side effects also remain: if the module changed global configuration, opened files, registered signal handlers or mutated singletons on import, none of that is undone. That is why unimporting is rarely a good primary design.

importlib.reload to refresh code

If you just want to re-run a module's code during development, importlib.reload states the intent more clearly:

python
1import importlib
2import mymodule
3
4mymodule = importlib.reload(mymodule)

Reloading has its own caveats, but it is the right tool when the need is "refresh the module implementation".

A new process for a clean import state

A new Python interpreter starts with an empty module cache, and that is the only complete reset. Test runners, subprocess-based tools and isolated worker processes give reliable isolation; manipulating sys.modules inside a long-running process does not.

Mistakes to avoid

  • Assuming del module_name unloads the module completely.
  • Removing an entry from sys.modules while other code still holds references to the module or its objects.
  • Expecting import-time side effects to be reversed automatically.
  • Using cache removal where importlib.reload expresses the intent better.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms