Python
Installed Modules
Local Environment
Python Libraries
Programming Tips

How do I get a list of locally installed Python modules?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

To effectively manage Python packages and understand their roles in your projects, it's crucial to know which modules are installed locally. Various methods serve different needs, ensuring both high-level overview and detailed insights are available. This article steps through the primary ways to list locally installed Python modules, providing context, examples, and even a table to summarize the key points.

Installed Python Modules

A Python module is essentially a package composed of various files, which include Python scripts, documentation, and other necessary resources. Installed modules can be critical for the execution of scripts by providing the necessary libraries.

Methods to List Installed Python Modules

1. Using pip list

pip is the package installer for Python, widely used for installing and managing Python packages. To view a list of all installed modules, run the following command:

bash
pip list

Example output:

 
1Package      Version
2------------ -------
3numpy        1.21.2
4pandas       1.3.3
5requests     2.26.0

This command provides a concise list of all installed modules along with their versions. It's useful for ensuring version compatibility and planning upgrades.

2. Using pip freeze

The pip freeze command is similar to pip list but outputs in a format compatible with requirements.txt files:

bash
pip freeze

Example output:

 
numpy==1.21.2
pandas==1.3.3
requests==2.26.0

This list can be redirected to a requirements.txt file to maintain a snapshot of your environment:

bash
pip freeze > requirements.txt

3. Using Python's help Function

Python's built-in help() function can be used to explore which modules are installed. In a Python shell, you can execute:

python
help("modules")

This lists all available modules, including built-in and third-party ones. Because it checks the environment directly, it's particularly useful for identifying modules dynamically available in interactive sessions, though it's less convenient for script usage.

4. Using the pkg_resources Module

For more advanced management, the pkg_resources module from setuptools provides direct access to installed distributions:

python
1import pkg_resources
2
3installed_packages = pkg_resources.working_set
4installed_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
5print(installed_list)

5. Using subprocess to Call pip from a Script

You can employ the subprocess module to call pip list from within a Python script, allowing integration into automated workflows:

python
1import subprocess
2
3result = subprocess.run(["pip", "list"], capture_output=True, text=True)
4print(result.stdout)

Summary Table

Here is a summary of the key points and data for the different methods outlined above:

MethodDescriptionOutput FormatSuitable For
pip listList installed packages with versionsTableQuick checks; version overview
pip freezeList installed packages in requirements.txt formatList with ==Environment snapshot; preparing a requirements.txt file
help("modules")List all available modules within the Python environmentTextInteractive exploration
pkg_resourcesAccess package data programmaticallyList in script outputAdvanced script-based management
subprocessRun pip list from within PythonAs pip listEmbedding package checks in automated processes

Conclusion

Understanding the installed modules in your Python environment is pivotal for maintaining stable and reliable software. Whether employing pip, leveraging Python's introspection capabilities, or integrating with larger systems, these tools equip developers with the necessary knowledge and control over their environment. This approach not only aids in development but also in debugging, package updates, and deploying consistent applications across different platforms.


Course illustration
Course illustration

All Rights Reserved.