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 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:
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:
A later import math can then load the module again. References held elsewhere are still not destroyed.
The closest "unimport": do both
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:
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_nameunloads the module completely. - Removing an entry from
sys.moduleswhile 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.reloadexpresses the intent better.
Related reading
- How to uninstall a package installed with pip install --user
- How to uninstall Python 2.7 on a Mac OS X 10.6.4?
- How to unnest explode a column in a pandas DataFrame, into multiple rows
- How to unpack pkl file
- How to unzip a folder in google colab?
- How to unzip a list of tuples into individual lists?
- How to update a plot in matplotlib
- How to update an existing Conda environment with a .yml file
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.