all Python 3systerm
sys.modules
modules
The dictionary of every loaded module, {name: module}. Python's import cache. A module can be removed from it to force a reload.
Syntax
sys.modules # dict[str, ModuleType]
This cache is why a module body runs exactly once: a second import in the same session will not pick up edits to the file. Deleting a key from sys.modules does not unload anything — the old classes and objects survive in references already taken, so after a re-import isinstance starts returning False for what looks like the same class. Use importlib.reload() for a reload, and restart the process when you need certainty.
Examples
import sys
import os
print('os' in sys.modules) # → True
print(type(sys.modules['os'])) # → <class 'module'>
print(len(sys.modules)) # → кол-во загруженных модулей
del sys.modules['os'] # убрали из кэша — сам модуль из памяти не выгружен
import os as os2
print(os2 is os) # → False — тело модуля выполнено заново, старый объект жив
import sys
import json
first = sys.modules['json']
import json # повторный import берёт модуль из кэша
print(sys.modules['json'] is first) # → True — тело модуля не выполнялось заново
See also
Open in the full glossarydocs.python.orgReport an inaccuracy