Skip to content

pip-audit vs Safety: Python dependency scanning as a CI gate

What pip-audit actually scans, how it differs from Safety on data source and licensing, and how to run it as a CI gate that fails honestly: pinned tool version, environment vs lockfile, --fix in CI, and suppressions with an expiry.

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-audit installed outside the project environment so its own dependencies never appear in your results: pipx install pip-audit, or pip install pip-audit inside 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 as safety scan and require you to authenticate with safety auth first; the older safety check path is deprecated.
  • A CI runner with outbound HTTPS to pypi.org (and api.osv.dev if 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.

Two pip-audit scan paths, a requirements file and an installed environment, converging on one package list matched against advisory data

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-auditSafety
MaintainerThe Python Packaging AuthorityA commercial vendor
Advisory sourcePyPI advisory API, or OSV with -s osvSafety’s own curated database
Account requiredNoYes — safety auth before scanning
Data licensingOpen, no usage restrictionFree tier is restricted to non-commercial use; commercial use needs a paid plan
Tool licenceApache-2.0MIT (the tool, not the data)
Output formatscolumns, JSON, Markdown, CycloneDX JSON and XMLscreen, JSON, HTML, SPDX
Expiring suppressionsNot built inBuilt into the policy file
OfflineCache helps; a first run needs the networkSame

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

  1. 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-audit
    No known vulnerabilities found

    That single line is what a clean run looks like. If pip-audit cannot 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 --strict to 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.

  2. 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.txt
    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

    The ID column carries real PYSEC- and GHSA- 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-audit resolves the constraints itself, which means it may audit a version you would never install. Add --no-deps to audit only the lines in the file, verbatim — useful for a lockfile that already contains every transitive pin.

  3. Read the output. Three columns matter. Version is what you pin today. Fix Versions is the smallest upgrade that clears the advisory — empty means no fix has shipped, which is the hard case. ID is what you paste into osv.dev to find out whether the vulnerable code path is one you call. Add --desc to print the advisory description inline, and --format json --output audit.json when a machine has to read the result.

    pip-audit -r requirements.txt --format json --output audit.json

    A 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.

  4. 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.

    Decision flow from a pip-audit finding to upgrade, expiring suppression, or a hard infrastructure failure

    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 3

    Exit 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.json

    Pinning pip-audit itself 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.

  5. Handle a suppression with an expiry. pip-audit accepts --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  payments

    Then 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 freeze on 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 Versions column 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-ignore with 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. --timeout sets the per-request limit; the exit-3 branch makes the failure legible.

  • --fix produced a diff nobody can review. pip-audit --fix -r requirements.txt rewrites 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-run locally 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.