pip-audit -r requirements.txt is the whole command. It resolves the packages you declared, asks PyPI’s advisory service which of those versions have published vulnerabilities, and prints a table. Getting that far takes ten minutes. The job that consumes real time is keeping that command green in a pipeline other people depend on: deciding whether it reads your lockfile or your running environment, what happens the morning a finding has no fixed version, and how a suppression stops being permanent. PyPA’s documentation covers the command well and does not cover any of that. This page is the second half.
What you’ll accomplish
- A dependency scan wired into CI as a blocking gate, running a pinned tool version so a tool upgrade never arrives as a surprise red build.
- A decision on whether you scan the resolved environment or the requirements file, and a way to see when the two disagree.
- A gate that tells “we found a vulnerability” apart from “the advisory service was unreachable”, and fails differently for each.
- A suppression list where every entry carries a review date and an owner, so the list shrinks instead of quietly growing.
- A clear boundary around what the scan proves, and where SBOM generation and container scanning take over.
Prerequisites
- Python 3.9 or newer, and a project you can install into a throwaway virtualenv.
pip-auditinstalled outside the project environment so its own dependencies never appear in your results:pipx install pip-audit, orpip install pip-auditinside a separate venv. It is Apache-2.0 licensed and maintained by the Python Packaging Authority.- Safety, if you want to compare:
pip install safety. Current Safety releases run assafety scanand require you to authenticate withsafety authfirst; the oldersafety checkpath is deprecated. - A CI runner with outbound HTTPS to
pypi.org(andapi.osv.devif you switch services). An air-gapped runner cannot run either tool without a mirror. - Ideally, a fully pinned requirements file. If you do not have one yet, start with pinning Python dependencies with pip-tools — a scan of unpinned requirements tells you far less than it appears to.
How it works
There are only three moving parts: a list of packages and versions, a database of advisories, and a match between them. What varies is where the list comes from.
Run pip-audit with no arguments inside an active virtualenv and it audits what is installed there — every transitive dependency pip actually resolved, with the extras and platform-specific wheels your machine happened to get. Run it with -r requirements.txt and it audits what the file declares, resolving anything the file left unpinned. Those are different lists, and the difference is not cosmetic.
On the advisory side, pip-audit queries the PyPI advisory API by default. Passing -s osv switches it to OSV, Google’s aggregated vulnerability database, which pulls in the same PyPI advisories plus other feeds. The identifiers you see are PYSEC- or GHSA- strings, and both resolve on osv.dev, which is where you go to read what the advisory actually says.
The practical consequence: a scan of a lockfile and a scan of the deployed environment can disagree, and when they do, the environment is telling you the truth about what you shipped. The lockfile is telling you what you meant to ship. Gate on the lockfile in CI, because that is the artifact a pull request changes, and scan the environment periodically as a drift check.
pip-audit vs Safety
The short answer: default to pip-audit, and do not run both as gates.
| pip-audit | Safety | |
|---|---|---|
| Maintainer | The Python Packaging Authority | A commercial vendor |
| Advisory source | PyPI advisory API, or OSV with -s osv | Safety’s own curated database |
| Account required | No | Yes — safety auth before scanning |
| Data licensing | Open, no usage restriction | Free tier is restricted to non-commercial use; commercial use needs a paid plan |
| Tool licence | Apache-2.0 | MIT (the tool, not the data) |
| Output formats | columns, JSON, Markdown, CycloneDX JSON and XML | screen, JSON, HTML, SPDX |
| Expiring suppressions | Not built in | Built into the policy file |
| Offline | Cache helps; a first run needs the network | Same |
The licensing row is the one that decides it for most teams. Safety’s tool is open source but its vulnerability data is not free for commercial use, which means running it in a company pipeline is a procurement conversation, not a pip install. pip-audit has no such condition, ships CycloneDX output that feeds straight into an SBOM workflow, and is maintained by the same group that maintains pip — so it will keep matching how pip resolves.
Safety earns its place if you already pay for it and want its policy file, which has a genuine expires field on each ignored advisory and a triage interface that non-engineers can read. That expiry mechanism is real and useful, and you can rebuild it around pip-audit in about fifteen lines of shell, which is what the rest of this page does.
What you should not do is gate on both. Two scanners means two flaky network calls, two suppression lists that drift apart, and half the attention paid to each finding. Pick one for the gate. If you want a second opinion, run it monthly as a report nobody can block a deploy with.
Step-by-step
-
Scan the environment you actually run. Activate the virtualenv and run the tool with no arguments. This is the honest picture of what is installed.
source .venv/bin/activate pip-auditNo known vulnerabilities foundThat single line is what a clean run looks like. If
pip-auditcannot resolve a dependency against PyPI — a private wheel, an editable install, an internal index — it prints a skip warning for that package and still exits zero. Add--strictto turn those skips into a failure, which is what you want in CI: a package silently excluded from the scan is worse than a package with a finding. -
Scan the requirements file instead. This is the CI gate’s input, because it is the file a pull request changes.
pip-audit -r requirements.txtFound 3 known vulnerabilities in 1 package Name Version ID Fix Versions -------- ------- -------------- ------------ urllib3 1.26.4 PYSEC-YYYY-NNN 1.26.5 urllib3 1.26.4 GHSA-XXXX-XXXX 1.26.17 urllib3 1.26.4 GHSA-XXXX-XXXX 1.26.18The ID column carries real
PYSEC-andGHSA-identifiers; they are shown as placeholders here because the exact set against any given pin changes as new advisories land. If the file is not fully pinned,pip-auditresolves the constraints itself, which means it may audit a version you would never install. Add--no-depsto audit only the lines in the file, verbatim — useful for a lockfile that already contains every transitive pin. -
Read the output. Three columns matter.
Versionis what you pin today.Fix Versionsis the smallest upgrade that clears the advisory — empty means no fix has shipped, which is the hard case.IDis what you paste into osv.dev to find out whether the vulnerable code path is one you call. Add--descto print the advisory description inline, and--format json --output audit.jsonwhen a machine has to read the result.pip-audit -r requirements.txt --format json --output audit.jsonA finding with a fix version is a fifteen-minute job: raise the pin, run the tests, open one commit that a reviewer can read. Resist the urge to automate that with
--fix. -
Wire it into CI as a gate. Three things separate a gate people trust from one they rerun until it passes: the tool version is pinned, the advisory cache is reused, and a tool failure does not look like a clean pass.
The exit code alone will not tell you why the run failed — findings and internal errors both produce a non-zero status. The reliable signal is whether a parseable report came out the other end:
#!/usr/bin/env bash # ci/audit.sh - fail on findings, fail differently on tool errors set -eo pipefail if pip-audit -r requirements.txt --strict \ --cache-dir .cache/pip-audit \ --format json --output audit.json; then echo "audit clean" exit 0 fi if [ -s audit.json ] && python3 -c 'import json; json.load(open("audit.json"))'; then echo "pip-audit reported findings - see audit.json" >&2 exit 1 fi echo "pip-audit produced no readable report - treat as infrastructure failure" >&2 exit 3Exit 1 is a real finding and blocks the merge. Exit 3 is your pipeline being broken, which is a different alert and a different owner. In GitHub Actions:
- uses: actions/setup-python@v5 with: python-version: '3.12' - name: Install the audit tool at a pinned version run: pip install pip-audit==2.7.3 - name: Reuse the advisory cache uses: actions/cache@v4 with: path: .cache/pip-audit key: pip-audit-${{ runner.os }}-${{ hashFiles('requirements.txt') }} - name: Audit declared dependencies run: ./ci/audit.sh - name: Keep the report either way if: always() uses: actions/upload-artifact@v4 with: name: pip-audit-report path: audit.jsonPinning
pip-audititself is not fussiness. A new release can start reporting advisories it previously skipped, and you want that to arrive as a deliberate bump you scheduled, not on the morning of a release. -
Handle a suppression with an expiry.
pip-auditaccepts--ignore-vuln ID, repeatable, and that flag has no expiry of its own — which is exactly how suppression lists become permanent. Put the expiry in a file the gate reads:# .pip-audit-ignore - advisory ID, review date, owner PYSEC-YYYY-NNN 2026-12-01 platform-team GHSA-XXXX-XXXX 2026-10-15 paymentsThen make a lapsed date fail the build on its own, before the scan even runs:
today=$(date -u +%F) flags=() while read -r id review owner; do [ -z "$id" ] && continue case "$id" in \#*) continue ;; esac if [[ "$review" < "$today" ]]; then echo "suppression for $id lapsed on $review (owner: $owner)" >&2 exit 2 fi flags+=(--ignore-vuln "$id") done < .pip-audit-ignore pip-audit -r requirements.txt --strict "${flags[@]}"The suppression now has a deadline someone has to answer for, and the file is in version control where the review date is visible in the diff. Ninety days is a reasonable default; anything longer and nobody remembers why the entry exists.
Validate it works
A gate you have never seen fail is not a gate. Prove it in a scratch environment with an old pin that certainly has advisories:
python3 -m venv /tmp/audit-check
/tmp/audit-check/bin/pip install --quiet 'urllib3==1.26.4'
pip-audit --path "$(/tmp/audit-check/bin/python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"
echo "exit code: $?"
Found 3 known vulnerabilities in 1 package
Name Version ID Fix Versions
-------- ------- -------------- ------------
urllib3 1.26.4 PYSEC-YYYY-NNN 1.26.5
urllib3 1.26.4 GHSA-XXXX-XXXX 1.26.17
urllib3 1.26.4 GHSA-XXXX-XXXX 1.26.18
exit code: 1
The count is whatever the advisory database holds on the day you run it — an old pin accumulates findings over time, so do not treat the number as fixed. What matters is the 1. Now run the same check through your CI script rather than by hand, and confirm the pipeline goes red. Then rm -rf /tmp/audit-check.
Troubleshooting
-
It passes locally and fails in CI. Your local venv and the CI runner resolved different versions. This is nearly always an unpinned transitive dependency, a platform marker that resolves differently on the runner’s OS, or an extra installed locally and not in CI. Compare with
pip freezeon both sides. The durable fix is a fully pinned lockfile plus--no-deps, so the scan reads the file rather than re-resolving it. -
A finding with no fix version. The
Fix Versionscolumn is empty and there is nothing to upgrade to. Read the advisory on osv.dev and decide whether your code reaches the vulnerable path. If it does not, add it to.pip-audit-ignorewith a review date and an owner. If it does, the answer is not a suppression — it is removing or replacing the dependency, or applying the mitigation the advisory describes. -
The job fails on a network error, not a finding. Advisory services time out. Without the parseable-report check in step 4, that failure looks identical to a real vulnerability and teams learn to hit rerun, which is how a gate becomes theatre.
--timeoutsets the per-request limit; the exit-3 branch makes the failure legible. -
--fixproduced a diff nobody can review.pip-audit --fix -r requirements.txtrewrites the file in place, and it upgrades every affected package at once with no test run in between. Never let it write in CI. Use--fix --dry-runlocally to see what it proposes, then make the changes through your normal pin-management flow so each bump lands as a reviewable commit. -
The suppression list has quietly grown. If nothing has ever been removed from
.pip-audit-ignore, the gate is passing on a list of things you decided not to look at. The expiry check in step 5 fixes this by construction: a lapsed date fails the build before the scan runs, so every entry gets re-argued on a schedule.
When this is the wrong approach
This scan answers one narrow question: do any of my declared Python dependencies have a published advisory today? It says nothing about your own code — no scanner here reads your handlers or your SQL. It says nothing about your OS packages, your base image, or anything installed outside pip; for that you want an image scan, and the SBOM plus Grype gate described here is the layer that covers it. And it says nothing about a package compromised last night, because a supply-chain attack with no advisory yet is invisible to every advisory database at once.
A green run means no known advisory matched your pins at that moment. It does not mean the project is secure, and treating it that way is how the gate becomes a false comfort. Pair it with pinned, hash-verified dependencies so the bytes you audit are the bytes you install, an update cadence that keeps you close to current instead of relying on advisories to force upgrades, and a scan of the runtime image for everything pip never touched. Full reference for the tool lives at pip-audit on PyPI.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.