Python
Python Modules
Programming
Software Development
Python Installation

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.

The fastest way to list every installed Python package is pip list. For a requirements-file-ready format, use pip freeze. If you need the list inside a running Python script, the importlib.metadata module (Python 3.8+) is the modern, recommended approach. Each method serves a different use case, so the right choice depends on whether you need a quick terminal check, a reproducible environment snapshot, or programmatic access.

Using pip on the Command Line

pip list

pip list prints every installed package with its version in a human-readable table:

bash
pip list

Output:

 
1Package    Version
2---------- -------
3numpy      1.26.4
4requests   2.31.0
5flask      3.0.2

You can filter for outdated packages with:

bash
pip list --outdated

This shows the current version, the latest available version, and the installation type, which is useful during dependency audits.

pip freeze

pip freeze produces output in the package==version format that pip install -r understands directly:

bash
pip freeze

Output:

 
flask==3.0.2
numpy==1.26.4
requests==2.31.0

To snapshot your environment into a requirements file:

bash
pip freeze > requirements.txt

A teammate or CI pipeline can then reproduce the exact same environment with pip install -r requirements.txt.

pip show (Single Package)

When you need details about one specific package rather than the full list:

bash
pip show requests

Output:

 
1Name: requests
2Version: 2.31.0
3Summary: Python HTTP for Humans.
4Home-page: https://requests.readthedocs.io
5Author: Kenneth Reitz
6License: Apache 2.0
7Location: /usr/local/lib/python3.12/site-packages
8Requires: certifi, charset-normalizer, idna, urllib3
9Required-by: httpx

The Requires and Required-by fields are especially helpful for understanding dependency chains before removing or upgrading a package.

Listing Packages Inside a Python Script

The importlib.metadata module is part of the standard library and replaces the older pkg_resources approach:

python
1from importlib.metadata import distributions
2
3installed = sorted(
4    [(d.metadata["Name"], d.version) for d in distributions()],
5    key=lambda x: x[0].lower()
6)
7
8for name, version in installed:
9    print(f"{name}=={version}")

This is faster than pkg_resources because it reads metadata lazily from .dist-info directories instead of eagerly scanning the entire working set at import time.

pkg_resources (Legacy)

For codebases stuck on Python 3.7 or earlier, pkg_resources from setuptools still works:

python
1import pkg_resources
2
3installed = sorted(
4    [f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set]
5)
6
7for package in installed:
8    print(package)

Be aware that pkg_resources has a measurable import-time cost (it scans all installed distributions on first import), so avoid it in performance-sensitive startup paths.

pkgutil (All Importable Modules)

pkgutil.iter_modules() lists every importable module on sys.path, including standard library modules and namespace packages that are not "installed packages" in the pip sense:

python
1import pkgutil
2
3for finder, name, ispkg in sorted(pkgutil.iter_modules(), key=lambda x: x[1]):
4    print(name)

Use this when you need to discover every module Python can import, not just those installed through pip.

Conda Environments

If you use Anaconda or Miniconda, conda list replaces pip list for packages installed through conda:

bash
conda list

For a cross-format export that includes both conda and pip packages:

bash
conda env export > environment.yml

This YAML file captures the conda channel, the exact package builds, and any pip-installed packages inside the environment.

Comparison of Methods

MethodCommand / CodeOutput FormatBest For
pip listpip listHuman-readable tableQuick terminal overview
pip freezepip freezepackage==versionRequirements files, CI reproducibility
pip showpip show <name>Detailed single-package infoDependency chain inspection
importlib.metadataPython scriptProgrammatic (name, version) tuplesModern scripts, fast metadata access
pkg_resourcesPython scriptProgrammatic string listLegacy Python 3.7 codebases
pkgutilPython scriptModule name listDiscovering all importable modules
conda listconda listTable with channel and build infoAnaconda/Miniconda environments

Virtual Environments and Scope

The list of installed packages depends entirely on which Python environment is active. Always activate your virtual environment before running any listing command:

bash
1# venv
2source .venv/bin/activate
3pip list
4
5# poetry
6poetry show
7
8# pipenv
9pipenv graph

Running pip list outside a virtual environment shows system-wide packages, which rarely reflects what your project actually uses.

Common Pitfalls

Mixing system Python with virtual environments. Running pip list without activating a venv shows system packages. Your project may depend on a package that exists system-wide but is missing from the project's own environment. Always check inside the activated venv.

Using pip freeze with globally installed tools. If you freeze a system Python that has linters, formatters, and CLI tools installed globally, your requirements.txt will include packages your application does not need. Use virtual environments to keep project dependencies isolated.

Relying on pkg_resources in new code. pkg_resources is not deprecated yet, but importlib.metadata is faster and has no external dependency. New projects should prefer importlib.metadata.

Forgetting --user installs. Packages installed with pip install --user go into a user-local directory and may not appear in a virtualenv's pip list. Check pip list --user if a package seems missing.

Assuming pip freeze and pip list show the same packages. pip freeze omits pip itself, setuptools, and wheel by default. pip list shows everything. This difference trips up developers who expect the two outputs to be identical.

Summary

  • pip list gives you a quick overview of all installed packages in the active environment.
  • pip freeze > requirements.txt creates a reproducible snapshot for deployment and collaboration.
  • importlib.metadata.distributions() is the modern, performant way to enumerate packages inside Python code.
  • Always activate your virtual environment before listing packages to avoid confusion between system and project dependencies.
  • For Conda users, conda env export captures both conda and pip packages in a single file.

Course illustration
Course illustration

All Rights Reserved.