sys.modules - Listing Loaded Modules

This section provides a tutorial example on how to list modules already loaded in the memory cache, using the 'sys.modules' attribute.

If you want to know how many modules have already been loaded in the memory cache, you can use the "sys.modules" attribute, which is a "dict" object holding a name-description pair for each loaded module.

The code below shows you an example of listing loaded modules.

# import a module from a module file
>>> import module_test

# list all loaded modules
>>> import sys
>>> for m in sys.modules:
...    print(f"name, description = {m}, {sys.modules[m]}")
...
name, description = builtins, <module 'builtins' (built-in)>
name, description = _io, <module 'io' (built-in)>
name, description = time, <module 'time' (built-in)>
name, description = zipimport, <module 'zipimport' (frozen)>
name, description = __main__, <module '__main__' (built-in)>
name, description = io, <module 'io' from
                    '/usr/lib/python3.8/io.py'>
name, description = google, <module 'google' (namespace)>
...
name, description = module_test, <module 'module_test' from
                    '/home/herong/module_test.py'>
name, description = sys, <module 'sys' (built-in)>

Obviously, all of those modules were loaded by the Python system, except the last two.

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

Modules and Module Files

 What Is Module

 "import module" - Two-Step Process

sys.modules - Listing Loaded Modules

 importlib.reload(module) - Reloading Module

 What Are Module Members

 "from module import member" Statement

 "from module import *" Statement

 What Is __all__ List

 __pycache__/module.version.pyc Files

 What Is the __main__ Module

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB