How do I get a list of locally installed Python modules?
Categories:
How to List Locally Installed Python Modules

Discover various methods to list all Python modules installed in your local environment, including those installed via pip, standard library modules, and user-defined packages.
Understanding which Python modules are installed in your environment is crucial for dependency management, debugging, and ensuring project compatibility. This guide explores several robust methods to retrieve a comprehensive list of your locally installed Python modules, catering to different needs and levels of detail.
Method 1: Using pip for Installed Packages
The pip
package installer is the most common tool for managing Python packages. It provides straightforward commands to list all packages it has installed. This method is ideal for quickly seeing third-party libraries that are part of your project's dependencies.
pip list
List all packages installed by pip.
The pip list
command outputs a table showing the package name and its version. For a more detailed view, including the location of each package, you can use pip show
for individual packages or combine it with pip freeze
.
pip freeze
List installed packages in a format suitable for requirements.txt.
pip freeze
command is particularly useful for generating a requirements.txt
file, which specifies your project's dependencies and their exact versions, ensuring reproducibility across different environments.Method 2: Inspecting sys.modules and sys.path for All Loaded Modules
For a more programmatic approach, Python's built-in sys
module offers insights into currently loaded modules and the paths Python searches for modules. This method can reveal modules that aren't necessarily installed via pip
, such as standard library modules or modules loaded dynamically.
import sys
print("--- Currently Loaded Modules ---")
for module_name in sorted(sys.modules.keys()):
print(module_name)
print("\n--- Python Search Paths (sys.path) ---")
for path in sys.path:
print(path)
Listing loaded modules and Python's search paths using sys
.
flowchart TD A[Start Python Interpreter] --> B{Import Statement Executed} B --> C{Check sys.modules} C -- Module Found --> D[Use Existing Module] C -- Module Not Found --> E{Search sys.path} E --> F[Load Module File] F --> G[Add to sys.modules] G --> H[Module Available]
How Python locates and loads modules.
sys.modules
is a dictionary that maps module names to actual module objects that have already been loaded. sys.path
is a list of strings that specifies the search path for modules. When you import
a module, Python searches these paths in order.
Method 3: Exploring site-packages Directories
Python packages are typically installed into site-packages
directories. You can manually inspect these directories to see what's installed. The location of site-packages
can vary depending on your operating system, Python version, and whether you're using a virtual environment.
import site
import os
print("--- Site-Packages Directories ---")
for sp_path in site.getsitepackages():
print(sp_path)
if os.path.exists(sp_path):
print(f" Contents of {sp_path}:")
for item in os.listdir(sp_path):
if item.endswith('.dist-info') or item.endswith('.egg-info') or os.path.isdir(os.path.join(sp_path, item)):
print(f" - {item}")
Programmatically finding and listing contents of site-packages.
site-packages
is generally discouraged. Always use pip
for installing, upgrading, or uninstalling packages to maintain consistency and avoid breaking dependencies.1. Identify your Python environment
Determine if you are in a virtual environment (e.g., venv
, conda
) or using the system-wide Python installation. This affects where modules are installed.
2. Use pip list
for installed packages
Run pip list
in your terminal to get a quick overview of third-party packages and their versions. This is the most common and recommended first step.
3. Inspect sys.modules
for loaded modules
For a runtime view of all modules currently loaded into your Python interpreter, including built-in and standard library modules, use the sys.modules
dictionary within a Python script.
4. Explore site-packages
for physical files
If you need to understand the physical location of installed packages, use site.getsitepackages()
to find the directories and then list their contents. Remember to use pip
for management, not direct file manipulation.