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.
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:
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.
A small Python script can then show the direct requirements for a selected package:
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:
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:
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:
- Use
pip show package-namefor quick direct requirements. - Use
pip inspect --localwhen you need structured analysis. - Build a reverse lookup before uninstalling or upgrading shared dependencies.
- 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 showreveals 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 showis good for quick direct dependency checks.' - '
pip inspectprovides 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.

