Python
pip
package management
dependencies
software development

Identifying the dependency relationship for python packages installed with pip

Master System Design with Codemia

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

Introduction

When a Python environment becomes crowded, it is easy to know that a package is installed but not know why it is there. Dependency inspection answers two useful questions: which packages does a distribution require, and which installed packages depend on it.

pip gives you part of that picture directly, and a few standard workflows fill in the rest. Once you understand those tools, dependency cleanup and upgrade planning become much safer.

Inspect Direct Requirements with pip show

The fastest way to inspect a package is pip show. It prints metadata including the packages listed in the distribution's Requires field.

bash
python -m pip show requests

Typical output includes fields such as Name, Version, Location, and Requires. That tells you the direct dependencies declared by requests, but it does not show the full dependency tree or which package pulled requests into the environment.

For a quick inventory of installed distributions, use:

bash
python -m pip list

That gives you the installed set, which is often the starting point for deeper inspection.

Use pip inspect for Structured Dependency Data

Recent versions of pip include pip inspect, which emits a JSON report describing the environment and installed distributions. That report is useful when you want to analyze dependencies programmatically.

bash
python -m pip inspect --local > inspect.json

A small Python script can then show the direct requirements for a selected package:

python
1import json
2
3with open("inspect.json", "r", encoding="utf-8") as f:
4    report = json.load(f)
5
6for dist in report["installed"]:
7    metadata = dist["metadata"]
8    if metadata["name"].lower() == "requests":
9        print("Package:", metadata["name"])
10        print("Requires:")
11        for requirement in metadata.get("requires_dist", []):
12            print(" -", requirement)

This is especially useful in CI, automated audits, or large environments where plain-text command output is harder to search reliably.

Find Reverse Dependencies

The harder question is often, "What depends on this package?" Standard pip commands are better at reporting outgoing requirements than reverse edges, so many developers use pip inspect output or a dedicated helper to answer that question.

For example, this script builds a reverse dependency map from the JSON report:

python
1import json
2from collections import defaultdict
3
4with open("inspect.json", "r", encoding="utf-8") as f:
5    report = json.load(f)
6
7reverse_map = defaultdict(list)
8
9for dist in report["installed"]:
10    name = dist["metadata"]["name"]
11    for requirement in dist["metadata"].get("requires_dist", []):
12        package_name = requirement.split(";", 1)[0].split("[", 1)[0].split(" ", 1)[0]
13        reverse_map[package_name.lower()].append(name)
14
15target = "urllib3"
16print(f"Packages depending on {target}:")
17for dependent in sorted(reverse_map[target]):
18    print(" -", dependent)

This tells you which installed packages list urllib3 as a requirement. That is the information you need before removing or force-upgrading a low-level library.

Use Third-Party Tree Views When Needed

If you want a visual dependency tree, many teams install pipdeptree in a development environment:

bash
python -m pip install pipdeptree
python -m pipdeptree

That tool is not part of standard pip, but it is widely used because it shows both the hierarchy and dependency conflicts clearly. It is often the fastest way to answer day-to-day questions, while pip inspect is better when you need machine-readable output.

Build a Reliable Workflow

A practical workflow looks like this:

  1. Use pip show package-name for quick direct requirements.
  2. Use pip inspect --local when you need structured analysis.
  3. Build a reverse lookup before uninstalling or upgrading shared dependencies.
  4. Prefer a virtual environment so the dependency graph stays scoped to one project.

That combination keeps dependency reasoning explicit. It also helps explain why deleting a package manually from site-packages is a bad idea: you lose the graph information that makes package management safe.

Common Pitfalls

  • Assuming pip show reveals reverse dependencies. It does not; it only shows direct requirements declared by the selected package.
  • Inspecting a global interpreter by accident. Always confirm which Python executable and environment you are using.
  • Forgetting environment markers and extras. Requirement strings can include conditions that only apply on certain platforms or Python versions.
  • Removing a low-level package because it looks unused. Another package may still depend on it indirectly.
  • Using stale dependency assumptions after upgrades. Re-run inspection after major package changes because the tree may have shifted.

Summary

  • 'pip show is good for quick direct dependency checks.'
  • 'pip inspect provides a structured JSON view of the installed environment.'
  • Reverse dependency analysis usually requires processing that JSON or using a helper such as pipdeptree.
  • Dependency inspection is safest inside a virtual environment tied to one project.
  • Understand both outgoing and incoming dependency edges before uninstalling or upgrading packages.

Course illustration
Course illustration