Python etc / sys.modules

Published: 17 September 2020, 18:00

Python caches every imported module is sys.modules:

import sys
import typing

# <module 'typing' from '/usr/local/lib/python3.7/'>

# 637

You can reload any module with importlib.reload to force it to be executed again. Be careful, though, since every object from the module will be recreated, you can break all isinstance checks and have hard times with debugging it.

old_list = typing.List
old_list is typing.List
# True

old_list is typing.List
# False