Python
Dependencies
Package Management
Programming
Software Development

How to find a Python package's dependencies

Master System Design with Codemia

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

Introduction

There are several ways to find a Python package's dependencies, and they answer slightly different questions. Sometimes you want the direct requirements declared by the package, and sometimes you want the full transitive tree actually installed in your environment.

Inspect Direct Requirements With pip show

If the package is already installed, the quickest command is pip show. Its output includes a Requires field listing the package's declared direct dependencies.

bash
python -m pip show requests

Typical output includes fields such as version, install location, and direct requirements. This is useful for a fast manual check, but it only shows what that package declares, not the full dependency graph below it.

Read Dependency Metadata in Python

If you want the same information from code, use importlib.metadata. This works well for tooling, scripts, and tests.

python
1from importlib.metadata import requires
2
3deps = requires("requests")
4
5for dep in deps or []:
6    print(dep)

The returned entries may include version specifiers, extras, and environment markers. For example, a dependency might only apply on a certain Python version or operating system.

This is often more reliable than scraping setup.py, because modern packages may define metadata in pyproject.toml and build it into wheel metadata instead of keeping everything in legacy setup files.

Inspect the Installed Dependency Graph

Direct requirements are not always enough. If you need to understand why a transitive package was installed or debug a resolver conflict, inspect the environment graph.

Recent versions of pip provide pip inspect, which outputs structured JSON about installed distributions:

bash
python -m pip inspect > inspect.json

That report is useful for tooling and automation because it describes installed packages and metadata in a machine-readable format. For a quick terminal view, many developers also use pipdeptree, though that is a third-party tool rather than part of pip itself.

Distinguish Source Metadata From Installed Reality

There is a difference between "what the project declares" and "what is currently installed." For example:

  • 'pyproject.toml or built metadata tells you declared requirements.'
  • 'pip show tells you installed package metadata.'
  • 'pip inspect helps you analyze the whole installed environment.'

That distinction matters when debugging. A package may declare a dependency range, but the resolver may install a specific version based on the rest of the environment.

Handling Extras and Environment Markers

Python dependencies can be conditional. A requirement may apply only when an extra is enabled or only on certain platforms.

python
1from importlib.metadata import requires
2
3for dep in requires("httpx") or []:
4    print(dep)

You may see entries with markers such as a Python version condition or optional extras. If you ignore those markers, you can misread the package's real dependency behavior.

Common Pitfalls

One common mistake is opening a source repository and assuming setup.py tells the full story. Many packages now use pyproject.toml, and the installed metadata is often the safer source of truth.

Another issue is confusing direct dependencies with transitive dependencies. pip show is fast, but it does not explain the entire dependency tree.

It is also easy to inspect the wrong environment. If your virtual environment is not activated, the dependency list you see may belong to a different interpreter entirely.

Summary

  • Use python -m pip show <package> for a quick view of direct installed requirements.
  • Use importlib.metadata.requires() when you want package dependency metadata from Python code.
  • Use python -m pip inspect when you need a machine-readable view of the installed environment.
  • Distinguish declared requirements from the fully resolved installed dependency graph.
  • Check extras, environment markers, and the active virtual environment before drawing conclusions.

Course illustration
Course illustration

All Rights Reserved.