Skip to content

Module 16 · External Libraries and APIs — topic 3 of 12

Dependencies

Direct and transitive dependencies, dependency trees, version constraints and conflicts, lock files, and keeping dependencies secure and up to date.

Beginner 8 min read·Lesson 237 of 306 in Python Programming

Learning objectives

  • Distinguish direct and transitive dependencies
  • Recognise version conflicts
  • Keep dependencies reproducible and secure

Definition

A dependency is a package your project needs. Direct dependencies are the ones you install yourself; transitive dependencies are the packages those depend on. Together they form a dependency tree that pip must resolve into one compatible set of versions.

Introduction

Installing requests also installs urllib3, idna, certifi and charset-normalizer. Your one-line requirement is really five packages — each with its own versions and updates.

Why it matters

Understanding the dependency tree explains install conflicts, bloated environments and security alerts about packages you never installed directly.

Detailed explanation

Direct dependencies appear in your requirements; transitive ones are pulled in automatically. pip show name lists a package's Requires and Required-by.

pip's resolver chooses one version of each package that satisfies every constraint in the tree. If no such version exists, installation fails with a conflict.

Conflicts arise when two packages need incompatible ranges of the same dependency, for example one requires urllib3 < 2 and another urllib3 >= 2.

A lock file pins every package in the tree (direct and transitive) to exact versions so installs are identical everywhere. pip freeze output, pip-tools and uv produce them.

Fewer dependencies mean fewer conflicts, smaller installs and less code to trust.

Vulnerabilities are often found in transitive dependencies. Tools such as pip-audit check installed versions against known security advisories.

Update regularly but deliberately: upgrade, run tests, then commit the updated lock file.

Syntax

Terminal
python -m pip show requests # Requires: certifi, charset-normalizer, idna, urllib3
python -m pip install pip-audit
python -m pip_audit # check for known vulnerabilities
  • pip show reveals the tree.
  • Lock files pin everything.
  • Audit dependencies regularly.

Code examples

Inspecting a dependency tree

Terminal
python -m pip show requests
python -m pip show urllib3 # look at its Required-by line

The exact versions shown depend on your environment.

Finding a compatible range

Python
def allowed(ranges):
low = max(r[0] for r in ranges)
high = min(r[1] for r in ranges)
return list(range(low, high))
# each package allows major versions [low, high) of a shared dependency
print(allowed([(1, 3), (2, 4)]))
print(allowed([(2, 3), (0, 2)]) or "conflict: no version satisfies both")
Output
[2]
conflict: no version satisfies both

More examples

Walking a dependency tree

Python
from graphlib import TopologicalSorter
deps = {
"myapp": ["requests", "rich"],
"requests": ["urllib3", "idna", "certifi", "charset-normalizer"],
"rich": ["pygments", "markdown-it-py"],
"markdown-it-py": ["mdurl"],
}
def all_deps(pkg):
seen = set()
stack = list(deps.get(pkg, []))
while stack:
d = stack.pop()
if d not in seen:
seen.add(d)
stack.extend(deps.get(d, []))
return sorted(seen)
print("direct:", deps["myapp"])
print("all:", all_deps("myapp"))
order = list(TopologicalSorter(deps).static_order())
print(order.index("mdurl") < order.index("markdown-it-py") < order.index("rich") < order.index("myapp"))
print(order[-1])
Output
direct: ['requests', 'rich']
all: ['certifi', 'charset-normalizer', 'idna', 'markdown-it-py', 'mdurl', 'pygments', 'requests', 'rich', 'urllib3']
True
myapp

Step-by-step explanation

  • Two direct dependencies expand to nine packages once transitive ones are included.
  • graphlib.TopologicalSorter (standard library) orders packages so each comes after everything it depends on — the order an installer needs.
  • The application itself comes last, after its whole tree.

Expected output

Running “Walking a dependency tree” prints exactly:

TextOutput
direct: ['requests', 'rich']
all: ['certifi', 'charset-normalizer', 'idna', 'markdown-it-py', 'mdurl', 'pygments', 'requests', 'rich', 'urllib3']
True
myapp

Practical applications

  • Security alerts from GitHub about transitive packages.
  • Install failures caused by two libraries needing incompatible versions.

Best practices

  • List every package you import directly.
  • Use a lock file for applications.
  • Audit and update dependencies regularly.

Common mistakes

Relying on transitive packages

Avoid

# requirements.txt lists requests only
import urllib3 # used directly in your code

Prefer

List urllib3 in requirements.txt too.

If requests stops depending on it, your import breaks.

Important notes

  • Some tools (uv, poetry, pip-tools) separate the short list of direct dependencies from the generated lock file.

Key takeaways

  • Dependencies form a tree of direct and transitive packages.
  • pip resolves one compatible version of each.
  • Lock, audit and update deliberately.

Try it yourself

  1. 1
    Run pip show on a package you use and draw its dependency tree two levels deep.
  2. 2
    Extend all_deps to report the depth at which each package first appears.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

Test yourself across the whole course in the Python practice bank.