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.
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
- pip show reveals the tree.
- Lock files pin everything.
- Audit dependencies regularly.
Code examples
Inspecting a dependency tree
The exact versions shown depend on your environment.
Finding a compatible range
[2] conflict: no version satisfies both
More examples
Walking a dependency tree
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:
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
Prefer
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
- 1Run pip show on a package you use and draw its dependency tree two levels deep.
- 2Extend 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.