A package on PyPI publishes a new version overnight, and your next pip install pulls it in without anyone looking. Python security does not need an expensive product, and to secure Python dependencies you can start with the package manager you already have. You need a handful of settings in your package manager. This tutorial uses uv for them: a lockfile with hashes, hash checking in pip, a waiting period for fresh releases, no building from source packages, and a vulnerability scan. I tested everything on 2 December 2026 with uv 0.13.9, Python 3.13.5, pip 25.1.1 and pip-audit 2.10.1. The outputs shown are real, though version numbers will differ on every run.
Where Python projects are exposed
Dependency management is easy to ignore until it bites: a typical Python project depends on a few direct and many indirect packages, the transitive dependencies. requests alone pulls in four more. Each of these Python dependencies carries three typical risks. First, a new release can be tampered with, for example after a maintainer account is taken over. We have covered the LiteLLM backdoor in PyPI packages and the tampered Microsoft Agent Framework releases as recent examples. Second, a source distribution (sdist) runs its setup.py when you install it, which means arbitrary code before you have imported anything. Third, an old version you never updated may already be publicly known as vulnerable without anyone noticing.
Each of these has a concrete setting that helps, and together they form a baseline for supply chain security in the Python ecosystem. None of them makes the supply chain safe on its own, but together they close the easiest paths for attackers on the Python supply chain. For a bill of materials of the finished project, see our SBOM tutorial with Syft and Grype. The JavaScript counterpart is a guide to how to secure npm dependencies.
How to secure Python dependencies, step 1: a lockfile with hashes
A lockfile records exactly which version of every package gets installed. uv stores it as uv.lock next to your pyproject.toml. I created an empty project and added requests:
uv init --bare --name hardening-demo
uv add requests
# + certifi==2026.11.20
# + charset-normalizer==3.5.3
# + idna==3.20
# + requests==2.34.2
# + urllib3==2.8.0
Example output; you will get newer versions depending on the date. For every file of a package, uv.lock contains a SHA-256 hash and the time it was uploaded to the index:
[[package]]
name = "certifi"
version = "2026.11.20"
source = { registry = "https://pypi.org/simple" }
wheels = [
{ url = "https://files.pythonhosted.org/.../certifi-2026.11.20-py3-none-any.whl", hash = "sha256:7422df25...488f", size = 127913, upload-time = "2026-11-20T23:37:48.117Z" },
]
A lockfile only helps if your build and deployment honor it. That is what --locked is for. I changed the requirement for urllib3 in pyproject.toml without updating the lockfile:
uv sync --locked
# error: The lockfile at `uv.lock` needs to be updated, but `--locked` was provided.
#
# hint: To update the lockfile, run `uv lock`.
That is the behavior you want for Python applications that you deploy: a hard failure instead of a silent new version. uv sync --frozen, by contrast, installs exactly what the lockfile says without checking whether it still matches pyproject.toml. In CI I use --locked because it also catches stale lockfiles. Commit the lockfile to your repository and review every change to it, because a changed hash or version in a pull request is exactly the signal you want to see.
Step 2: enforce hashes with pip
Not every project uses uv everywhere. For pip in a plain venv and other tools, uv can export a requirements.txt with hashes:
uv export --format requirements-txt --no-emit-project -o requirements.txt
head -6 requirements.txt
# certifi==2026.11.20 \
# --hash=sha256:7422df25243767b9877031a2248cb14d09ddac9beee846f4cef3d5c9191d488f \
# --hash=sha256:af1cb240a8133f105aa52684a3a3258339d7f2fd0bd4e2812698ff5cf5588c4e
# # via requests
pip verifies these hashes as soon as the file contains at least one hash or you pass --require-hashes. To prove the check works, I changed both hashes of certifi in a copy of the file and installed it into a fresh venv:
pip install --require-hashes -r requirements_tampered.txt
# ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE.
# certifi==2026.11.20 from https://files.pythonhosted.org/.../certifi-2026.11.20-py3-none-any.whl
# Expected sha256 0422df25243767b9877031a2248cb14d09ddac9beee846f4cef3d5c9191d488f
# Expected or 0f1cb240a8133f105aa52684a3a3258339d7f2fd0bd4e2812698ff5cf5588c4e
# Got 7422df25243767b9877031a2248cb14d09ddac9beee846f4cef3d5c9191d488f
uv pip install --require-hashes -r requirements_tampered.txt gives the same result with the message Hash mismatch for certifi==2026.11.20. One detail surprised me at first: when I had changed only a single hash, the install went through. For each package, it is enough that one of the listed hashes matches, because the wheel and the source archive each have their own. A hash protects you against a file changing after the fact, not against an author who shipped malicious code from the start. Provenance, meaning proof of who built a package and from which source, is a separate topic that I did not test here. The pip documentation on secure installs explains the background of this mode.
Step 3: delay new releases with exclude-newer
The idea behind a delay is simple: when a tampered release is noticed, the package index usually removes it. If you only accept versions that have been available for a while, you have a better chance that this has already happened before you install. uv has exclude-newer for this. Besides fixed dates it accepts relative values such as 7 days, as I read in the uv settings reference and confirmed by testing. It is compared against the upload time of each individual file. I re-locked the same project with different delays (as of 2 December 2026):
uv lock --exclude-newer "14 days"
# certifi 2026.10.23, charset-normalizer 3.5.2, idna 3.20, requests 2.34.2, urllib3 2.8.0
uv lock --exclude-newer "90 days"
# certifi 2026.7.22, charset-normalizer 3.5.1, idna 3.19, requests 2.34.2, urllib3 2.7.0
Without a delay, certifi 2026.11.20 was the latest on the package index; with 14 days it became 2026.10.23. To make this permanent for everyone on the team and in CI, put it into pyproject.toml:
[tool.uv]
exclude-newer = "7 days"
uv writes the span into the lockfile as exclude-newer-span = "P7D". The delay is a trade-off, because it also slows down legitimate security fixes. For an urgent patch, lift it for a single package with --exclude-newer-package instead of loosening the rule for everything.
Step 4: do not build source packages
A wheel is already built, and installing it just unpacks files. A source distribution has to be built, and that runs the package’s code. To demonstrate, I built a harmless package called demopkg whose setup.py writes a file to /tmp, and installed it twice:
uv pip install --no-build demopkg-0.1.tar.gz
# error: Building source distributions is disabled, but attempted to build `demopkg`
uv pip install demopkg-0.1.tar.gz
# + demopkg==0.1 (from file:///.../demopkg-0.1.tar.gz)
cat /tmp/setup_py_was_here.txt
# setup.py executed
Without --no-build, the setup.py ran during installation. With the option, uv refuses to build. In a project you set it permanently:
[tool.uv]
no-build = true
The consequence shows up when you add a package that only exists as a source distribution. For uv add docopt, uv reported:
error: Failed to add dependencies
cause: No solution found when resolving dependencies
cause: Because all versions of docopt have no usable wheels and your project depends on docopt, we can conclude that your project's requirements are unsatisfiable.
That is not a bug but the point: you decide deliberately which packages may run foreign code at build time. Use no-build-package to relax the rule for a single package instead of switching it off globally. Note that even a wheel can contain malicious code that only runs on import. This setting prevents execution at install time, not at runtime.
Step 5: scan for vulnerabilities with uv audit and pip-audit
Scanning checks your Python packages against vulnerability databases such as OSV. A vulnerability scanner can only report what has already been published. uv ships uv audit for this; in uv 0.13.9 the command is still marked experimental and prints a warning. I deliberately pinned outdated packages (requests==2.19.1):
uv audit
# Found 19 known vulnerabilities and no adverse project statuses in 5 packages
# idna 2.7 has 2 known vulnerabilities:
# requests 2.19.1 has 5 known vulnerabilities:
# urllib3 1.23 has 12 known vulnerabilities:
echo $?
# 1
Exit code 1 makes a CI pipeline fail. Fixing it is the interesting part. I raised the requirement to requests>=2.34 and re-locked. Two vulnerabilities in idna 2.7 remained, because uv keeps already locked versions of Python packages as long as they satisfy the new requirements. Only a targeted upgrade cleaned it up:
uv lock --upgrade-package idna
# Updated idna v2.7 -> v3.20
uv audit
# Found no known vulnerabilities and no adverse project statuses in 5 packages
If you prefer an established tool, use pip-audit. I ran uvx pip-audit -r old.txt --no-deps --disable-pip on a file with the same old versions. It reported 41 vulnerabilities in four packages, more than uv audit. Some entries appeared twice in the table, for example PYSEC-2018-28. Counts from different scanners are therefore not directly comparable, and every scanner needs a way to deal with false positives. uv audit offers --ignore and --ignore-until-fixed for that, which I found in its --help but did not use here. What matters is that you run one of them regularly, in the same workflow that installs your packages. The database behind both is OSV.
All of it as a CI recipe
For a project managed with uv, these entries in pyproject.toml and two commands in the pipeline are enough:
[tool.uv]
exclude-newer = "7 days"
no-build = true
uv sync --locked
uv audit --locked
This configuration and both commands ran without errors in my test project. --locked makes a stale lockfile stop the pipeline. You can also adopt the settings one at a time, since each works on its own. For pip and pip-tools you are left with the hash export from step 2 and none of the uv options. I did not test whether Poetry or other tools offer similar options.
What this setup does not do
Hashes and a lockfile give you reproducibility, not package quality, so treat them as one best practice among several rather than a finished concept for dependency security. A tampered release that you locked on your first uv lock stays tampered. The delay lowers that risk but does not rule it out if a release goes unnoticed for longer. A scan only finds vulnerabilities that already sit in a database. And uv audit is, as said, still experimental, so its output format may change. The goal is therefore a realistic one: fewer surprises from foreign code, versions you can trace, and an alarm when a known vulnerability sits in your packages.




