sys.modules: How Python caches already-loaded modules

sys.modules: How Python caches already-loaded modules

In Python, import always defines a variable. But it only loads the module once.

sys.modules, a dict, tracks already-loaded modules:

– keys are strings, the module names
– values are module objects

If you say “import pandas as pd”, sys.modules has a key ‘pandas’, not ‘pd’.