Dependencies
Last updated
Pila's own code is a few hundred lines. Under it are thousands of lines written by strangers: a web framework, a database library, the libraries those use, and so on down. That's how modern software is built, and it's also a risk: code you didn't write can break, vanish, or be turned against you. In this lesson you'll pin Pila's dependencies, update them safely, and see two real incidents that show what can go wrong.
By the end of this lesson you can
- Declare and pin a project's dependencies with pip and requirements files
- Read version numbers and ranges, and update without surprises
- Describe supply-chain risks and the habits that reduce them
Other people’s code
Pila doesn't write its own web server, database driver or HTTPS code. It uses packages: code published by others, installed from the Python Package Index (PyPI) with pip. The packages Pila uses directly are its dependencies; the packages they use are transitive dependencies. Pila asks for 3 and gets 19.
That's normal, and good: those packages are tested by thousands of projects. But each one is code Pila trusts without having written it. Simple Python's Python on your computer showed pip and virtual environments; here's what changes when a team and a live service depend on them.
Declaring and pinning
A project lists its dependencies in a file, so everyone installs the same ones. A version specifier says which versions are acceptable:
| Written | Means | Risk |
|---|---|---|
flask | Any version, whatever is newest today | Next month’s install may be different, and break |
flask>=3.0 | 3.0 or anything newer | A future 4.0 with breaking changes would be accepted |
flask~=3.1 | 3.1 or newer, but below 4 (a “compatible release”) | Takes fixes and features, not major changes |
flask==3.1.1 | Exactly this version | Nothing changes until you change it |
Many teams keep two files: one listing what they need, with ranges, and a lock file listing every package, transitive ones too, pinned to the exact version (and often a hash of the file, so a swapped file is refused). The lock file is what gets installed, on every laptop and on the server.
python -m pip install -r requirements.txt python -m pip freeze > requirements.lock python -m pip install -r requirements.lock
pip freeze writes every installed package with its exact version. Tools such as pip-tools and uv do this more carefully; the idea is the same.
Updating, and what breaks
Many packages number their versions major.minor.patch, following Semantic Versioning: a patch (3.1.1 → 3.1.2) fixes bugs; a minor (3.1 → 3.2) adds features without breaking anything; a major (3 → 4) may break code that used the old version. It's a promise, not a guarantee.
Pila's team updates on purpose, regularly: once a month, one package at a time, on a branch, with the tests running (Stage 6). Security fixes go in as soon as they're announced. Updating little and often keeps each step small; updating once every two years is a leap nobody wants to make.
Two incidents
| left-pad, 2016 | xz utils, 2024 | |
|---|---|---|
| What it was | An 11-line JavaScript package that pads text with spaces | A compression library inside many Linux systems |
| What happened | After a dispute over a different package’s name, its author removed all his packages from npm | Over about two years, a contributor won the maintainers’ trust, then hid a backdoor in versions 5.6.0 and 5.6.1 |
| The effect | Thousands of projects that depended on it, directly or through others, failed to build | A way into SSH logins on some Linux systems (CVE-2024-3094) |
| How it ended | npm restored the package within hours, and changed its rules on removing packages | An engineer noticed SSH logins were half a second slow, and investigated. It was caught before most stable releases shipped it |
| The lesson | You depend on things you’ve never heard of, deep in the tree | Trust in a package is trust in the people who publish it |
The US Cybersecurity and Infrastructure Security Agency told everyone to go back to an earlier, uncompromised version of xz. Teams with an up-to-date list of their components knew within minutes whether they were affected.
Habits that protect you
No habit removes the risk, but several lower it. A software bill of materials (SBOM) lists every component in a piece of software, so you can answer "do we use it?" at once. SLSA (Supply-chain Levels for Software Artifacts) is a framework for showing that a package was built from the source it claims, and many registries now support signed releases. Simple Cybersecurity's lesson 4.8 goes deeper. Which of these habits lower the risk?
0 of 7 placed right
Committing a lock file, so every install gets exactly the same versions
Copying an install command from a forum post without checking the package name
Updating one dependency at a time, on a branch, with the tests running
Adding a package to save writing ten lines
Getting alerts when a dependency has a published vulnerability
Never updating anything, so nothing breaks
Keeping a list of every component (an SBOM)
Spot the trap: a name that looks right
Jun needs to make web requests from a script and types pip install reqeusts. It installs, the script runs, and he moves on.
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- pip: Repeatable Installs: pinning, hashes and lock files
- Python Packaging User Guide: Version specifiers: ==, >=, ~= and the rest
- npm Blog: kik, left-pad, and npm (2016): npm’s own account of the left-pad removal
- NIST National Vulnerability Database: CVE-2024-3094: malicious code in xz versions 5.6.0 and 5.6.1
- CISA: Reported Supply Chain Compromise Affecting XZ Utils (2024): the US alert, and its advice to downgrade
- CISA: Software Bill of Materials (SBOM): a list of every component in a piece of software
- SLSA: Supply-chain Levels for Software Artifacts: a framework for trustworthy builds