Building it · lesson 7 of 10
Ideas in this lesson
Arequirements.txtthe packages a project needs, and which versions
B==, >=, ~=pin one version, or allow a range
CLock fileevery package, exactly, including the ones yours use
DSupply chaineverything your software is built from

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:

Version specifiers
WrittenMeansRisk
flaskAny version, whatever is newest todayNext month’s install may be different, and break
flask>=3.03.0 or anything newerA future 4.0 with breaking changes would be accepted
flask~=3.13.1 or newer, but below 4 (a “compatible release”)Takes fixes and features, not major changes
flask==3.1.1Exactly this versionNothing 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.

Terminal
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

When the supply chain broke
left-pad, 2016xz utils, 2024
What it wasAn 11-line JavaScript package that pads text with spacesA compression library inside many Linux systems
What happenedAfter a dispute over a different package’s name, its author removed all his packages from npmOver about two years, a contributor won the maintainers’ trust, then hid a backdoor in versions 5.6.0 and 5.6.1
The effectThousands of projects that depended on it, directly or through others, failed to buildA way into SSH logins on some Linux systems (CVE-2024-3094)
How it endednpm restored the package within hours, and changed its rules on removing packagesAn engineer noticed SSH logins were half a second slow, and investigated. It was caught before most stable releases shipped it
The lessonYou depend on things you’ve never heard of, deep in the treeTrust 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

  1. Committing a lock file, so every install gets exactly the same versions

  2. Copying an install command from a forum post without checking the package name

  3. Updating one dependency at a time, on a branch, with the tests running

  4. Adding a package to save writing ten lines

  5. Getting alerts when a dependency has a published vulnerability

  6. Never updating anything, so nothing breaks

  7. Keeping a list of every component (an SBOM)

Spot the trap: a name that looks right

Spot the trapJun

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

What does a lock file list?
A package goes from 3.1.4 to 4.0.0. Under Semantic Versioning, what should you expect?
What did the xz incident show?

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.

All sources for Simple Software Development