The opposite of import.
pip install deport
import os
import json
deport os, json
# os and json are now completely unloaded from sys.modules and deleted from scope
import os # re-importing works — loads the module freshThat's it. No special imports, no decorators, no configuration. Just pip install deport and the deport statement is available in all your Python files.
deport completely reverses an import:
- Removes the module (and all submodules) from
sys.modules - Deletes the name from the current scope
This means re-importing after a deport will reload the module from scratch, rather than returning the cached version.
When you pip install deport, a .pth file is installed into site-packages that activates the deport machinery at Python startup. It uses three mechanisms:
- Import hook: transparently preprocesses imported
.pyfiles, rewritingdeport Xinto valid Python before compilation - Excepthook: catches
SyntaxErrorin the main script, preprocesses the source, and re-executes it - Custom codec: files with
# coding: deportare preprocessed at decode time
deport os # single module
deport os, json # multiple modules
deport os.path # submodule (also removes os from scope)Why not?