Lesson 6 of 20 · 18m
Dependencies: Pin What You Actually Run
Ranges resolve at install time — lock files, --require-hashes, and the PyPI supply-chain incidents that made pinning a security control.
pip install "requests>=2.31" does not install requests. It installs whatever the resolver selected that day — today 2.32.3, next month something newer — plus every transitive dependency it drags along. Two builds of the same commit, a week apart, run different code. The wrong mental model is "the dependencies in pyproject.toml are what my app runs." They are constraints on a search. The lock file is the answer.
Libraries publish ranges; applications lock
A library's job is to coexist: dependencies = ["requests>=2.31,<3"] declares what it tolerates. An application's job is to be reproducible: every transitive package pinned to an exact version, ideally to an exact artifact. pip-tools (pip-compile requirements.in -o requirements.txt --generate-hashes) produces a fully pinned tree with a sha256 per wheel; uv lock writes uv.lock; poetry writes poetry.lock. Commit the lock file.
uv sync and pip install -r requirements.txt --require-hashes install exactly the recorded tree — --require-hashes refuses any artifact whose file doesn't match, closing the gap where a version stayed the same but the bytes behind it changed.
Why anyone bothers: the registry is adversarial
December 2022: PyTorch's nightly builds referenced torchtriton, a dependency published only to PyTorch's own index. Someone registered the name on public PyPI and shipped a package that exfiltrated /etc/passwd, /etc/hosts, SSH keys, and environment variables off every machine that installed nightly. That is dependency confusion — Alex Birsan demonstrated the class in 2021 by beating internal package names at Apple, Microsoft, and PayPal with public uploads — and it works because resolvers compare names and versions, not provenance. A CI step passing --index-url <private> --extra-index-url https://pypi.org/simple lets the public index win whenever it publishes a higher version of your internal name.
May 2022: the ctx package's maintainer email domain expired. An attacker registered the domain, received the PyPI password-reset email, and shipped malicious versions that stole AWS credentials — the same story hit phpass. PyPI made 2FA mandatory for maintainers in 2023 largely because of that takeover class. Typosquatting — reqeusts, homoglyph lookalikes — never stopped; PyPI removes hundreds of malicious packages a year and still misses a steady stream.
The practice
- Applications: lock file committed;
--require-hashes(pip) oruv sync(which always verifies) for deploy installs. Nobody installs loose ranges in production. - Private packages: one index. Never mix
--extra-index-url https://pypi.org/simplewith a private index. uv's[[tool.uv.index]]withexplicit = truebinds a package name to one index. - Audit in CI:
pip-auditchecks the installed tree against the OSV advisory database and readsuv.locktoo. A new CVE in a pinned dep should fail the build, not wait for a human to notice. - Updates are deliberate: Dependabot or Renovate PRs, and a version bump gets reviewed like any code change — read the package's diff, not just yours. The torchtriton attacker shipped a "newer" version; nothing about a bump is safe by default.
- Pin
requires-python, and review the lock file under the same rigor as the code.
Expect the transitive CVE: the advisory that fires in pip-audit is almost always two levels below what you declared — the dep of your dep. That is the other reason the lock file earns its place: it is the only artifact listing what you actually run.
The tree is bigger than your list
Your dependencies are the visible tip. uv tree or pipdeptree prints the full resolved tree — a project declaring six direct deps routinely resolves forty or more transitives. Each of those is code running in your process that you never picked. ResolutionImpossible — the resolver's error when ranges conflict — is the constraint solver telling you the intersection is empty, not that it failed.
Ranges describe tolerance; the lock describes reality. The lock file is the artifact that makes two builds identical.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Free-Threaded Python, Measured: Threads Finally Scale, and Your Unlocked Counter Finally Breaks
Python 3.14 made the no-GIL build officially supported, and 3.15 kept going. I ran the same CPU-bound job on standard and free-threaded builds of both: threads went from slower-than-serial to 1.9x faster, and a counter that never lost an update under the GIL lost 70% of them without it.
What is Python? Enterprise, Data Science, and the Backend of the Web
Python runs Instagram, Dropbox, Netflix, Spotify, and Reddit. It trains the models that power modern AI. It processes petabytes of data in enterprise pipelines. And I ignored it for years because I thought it was just for academics. Here is what changed my mind.
What Python 3.13 Shipped
The first release of Python 3.13 is now here as a Beta Release. This article is a rundown of all of the important changes and features that are being released in Python 3.13, along with what they mean for Python Developers. Please remember that this can change between
What Python 3.11 Actually Changed
The Python Programming Language releases a new version every year, with a feature-locked beta release in the first half of the year and the final release toward the end of the year. The feature set for Python 3.11 has just been finalized, with a beta version available for testing.