Comprehensive Guide to Listing Installed Packages and Their Versions in Python

Nov 20, 2025 · Programming · 12 views · 7.8

Keywords: Python package management | pip freeze | pip list | virtual environment | dependency management

Abstract: This article provides an in-depth exploration of various methods to list installed packages and their versions in Python environments, with detailed analysis of pip freeze and pip list commands. It compares command-line tools with programming interfaces, covers virtual environment management and dependency resolution, and offers complete package management solutions through practical code examples and performance analysis.

Introduction

Understanding installed packages and their versions in Python environments is crucial for development workflows. This knowledge facilitates project management, dependency conflict resolution, and version compatibility assurance. This article systematically introduces multiple approaches to list Python packages while analyzing their respective application scenarios.

Using pip Command-Line Tools

pip, as Python's official package manager, provides two primary commands for package listing: pip freeze and pip list.

Detailed Analysis of pip freeze

The pip freeze command generates a formatted package list with each package following the package==version format. This output is particularly suitable for requirements.txt file generation:

pip freeze > requirements.txt

Sample output from this command:

numpy==1.21.0
pandas==1.3.0
requests==2.26.0
Django==3.2.0

Analysis of pip list Command

The pip list command provides more user-friendly tabular output with package names and version columns:

pip list

Output format example:

Package    Version
---------- ----------
numpy      1.21.0
pandas     1.3.0
requests   2.26.0
Django     3.2.0

Command Comparison and Selection

While both commands list installed packages, significant differences exist:

Programmatic Package Listing

Beyond command-line tools, package information can be retrieved programmatically through Python code.

Using pkg_resources Module

The pkg_resources module provides interfaces for accessing package metadata:

import pkg_resources

installed_packages = pkg_resources.working_set
packages_list = sorted([f"{pkg.key}=={pkg.version}" for pkg in installed_packages])
for package in packages_list:
    print(package)

This approach enables dynamic package information retrieval within Python scripts, facilitating integration with automation tools.

Using help('modules') Method

Python's built-in help('modules') command lists all available modules:

help('modules')

However, this method presents limitations:

Package Management in Virtual Environments

When managing packages in virtual environments, specific commands ensure only current environment packages are listed:

pip list --local

This command excludes system-level installed packages, displaying only those installed in the current virtual environment, maintaining environment isolation.

Alternative Package Management Tools

Pipenv Environments

For projects managed with Pipenv, package lists can be generated via:

pipenv lock -r

This command produces precise dependency lists based on Pipfile.lock files, ensuring environment consistency.

Conda Environments

In Anaconda or Miniconda environments, use the conda command:

conda list

This lists all packages installed via conda, including Python packages and non-Python dependencies.

Performance Analysis and Best Practices

Execution Efficiency Comparison

Significant efficiency differences exist among methods:

Best Practice Recommendations

  1. Project Dependency Management: Use pip freeze > requirements.txt for dependency固化
  2. Daily Inspection: Use pip list for quick environment status checks
  3. Automation Scripts: Use pkg_resources programming interface
  4. Environment Isolation: Use --local option in virtual environments

Advanced Application Scenarios

Dependency Relationship Analysis

Combining multiple tools enables in-depth dependency analysis:

import pkg_resources
from pip._internal.utils.misc import get_installed_distributions

# Retrieve installed packages and their dependencies
distributions = get_installed_distributions()
for dist in distributions:
    print(f"Package: {dist.key}=={dist.version}")
    for requirement in dist.requires():
        print(f"  Requires: {requirement}")

Version Compatibility Checking

Developing custom tools for package version compatibility verification:

import pkg_resources

def check_compatibility():
    installed = {pkg.key: pkg.version for pkg in pkg_resources.working_set}
    
    # Define compatibility rules
    compatibility_rules = {
        'numpy': '>=1.20.0',
        'pandas': '>=1.3.0',
    }
    
    for package, rule in compatibility_rules.items():
        if package in installed:
            if not pkg_resources.safe_version(installed[package]) in pkg_resources.Requirement.parse(f"{package}{rule}"):
                print(f"Warning: {package} version {installed[package]} may not be compatible")

Conclusion

Python offers multiple flexible approaches to list installed packages and their version information. Developers should select appropriate methods based on specific requirements: command-line tools suit daily use and script integration, while programming interfaces facilitate custom tool development. Understanding the characteristics and limitations of various methods enables more effective Python project dependency management, ensuring environment stability and reproducibility. As the Python ecosystem continues evolving, package management tools undergo continuous improvement, recommending consultation of official documentation for latest best practices.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.