Skip to main content

Security and hardening · Beginner · 16 min

Find vulnerable Python packages with pip-audit

By Padding · Updated 18 Nov 2026
Debian (python:3.15-slim) · Python 3.15 · pip-audit 2.10.1 · uv 0.13.10

You will audit a small Python project with pip-audit 2.10.1 through its requirements file, its virtual environment and an exported uv.lock, fix the findings, record one accepted risk with a written reason, save JSON and CycloneDX reports, and add the audit to CI.

On this page

Most of the code in a Python project is not yours. It arrives through pip install or uv add, and every package you pin today can have a published security flaw tomorrow. pip-audit is the Python Packaging Authority's tool for spotting that. It reads the exact versions you use, asks an advisory database whether any of them have known vulnerabilities, and tells you which version fixes each one.

In this tutorial you build a tiny project that renders a template with an old release of Jinja2, 3.1.2, which has several public advisories. You audit it three ways (a requirements file, a virtual environment and a uv lock file), read what the results mean, fix them, record one accepted risk properly, save reports for your records and make the check run in CI.

pip-audit finds known vulnerabilities in Python packages. It does not read your code, it does not spot malicious packages, and it does not know about flaws in system libraries a package happens to load. Treat it as one check among several.

Step 1 · Install pip-audit as a tool

Install pip-audit with uv as a standalone tool. uv gives it its own private environment, so its dependencies never mix with your projects, and the exact version pin means everyone on your team runs the same release:

Bash
uv tool install pip-audit==2.10.1
pip-audit --version

If your shell says pip-audit: command not found, run uv tool update-shell and open a new terminal. It adds uv's tool folder (~/.local/bin on Linux and macOS) to your PATH.

Check it worked

The second command prints pip-audit 2.10.1.

Step 2 · Create a demo project with an old dependency

Make a throwaway project folder with a requirements file pinned to the old Jinja2, and a script that uses it:

Bash
mkdir -p ~/pip-audit-demo
cd ~/pip-audit-demo
cat > requirements.txt <<'TXT'
jinja2==3.1.2
markupsafe==3.0.4
TXT
cat > app.py <<'PY'
from jinja2 import Environment

env = Environment(autoescape=True)
template = env.from_string("Hello {{ name }}")
print(template.render(name="<Ada>"))
PY

Both lines use ==, so the file names one exact version of each package. That matters for auditing: pip-audit can only give a firm answer about versions it knows for certain. A range such as jinja2>=3 has to be resolved first, and the answer then depends on the day you run it.

Check it worked

cat requirements.txt shows the two pinned lines, and ls shows app.py and requirements.txt.

Step 3 · Audit the requirements file

Point pip-audit at the file with -r:

Bash
pip-audit -r requirements.txt || echo "pip-audit exit code: $?"

pip-audit creates a temporary environment, works out the full set of packages the file would install, then looks each one up. It exits with code 1 when it finds anything and 0 when it finds nothing. That exit code is what makes it useful in scripts and CI; the || part here prints the code instead of stopping a script.

The output is a table along these lines (the order of rows may differ):

Text
Found 5 known vulnerabilities in 1 package
Name   Version ID              Fix Versions
------ ------- --------------- ------------
jinja2 3.1.2   PYSEC-2026-1473 3.1.3
jinja2 3.1.2   PYSEC-2026-1474 3.1.4
jinja2 3.1.2   PYSEC-2026-1475 3.1.5
jinja2 3.1.2   PYSEC-2026-1472 3.1.5
jinja2 3.1.2   PYSEC-2026-1471 3.1.6
pip-audit exit code: 1

Five advisories were listed for Jinja2 3.1.2 when this was written; new ones can appear at any time, which is the point of running the audit regularly. MarkupSafe 3.0.4 is not in the table because it has none.

One warning from the pip-audit documentation is worth repeating: auditing a requirements file downloads and inspects the same packages pip install -r would. Only audit files you would be willing to install.

Check it worked

You see rows for jinja2 3.1.2 with IDs and fix versions, a Found ... known vulnerabilities line, and pip-audit exit code: 1.

Step 4 · Audit an active virtual environment

A requirements file says what you meant to install. The virtual environment holds what is actually installed, which can drift when someone runs pip install by hand. Create one, install the file into it and activate it:

Bash
cd ~/pip-audit-demo
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --quiet --upgrade pip
python -m pip install --quiet -r requirements.txt
python app.py

pip-audit audits the Python interpreter it runs under. Because you installed it as a uv tool, that is its own private environment, not your project's. If you run plain pip-audit with a project environment active, it prints a warning saying exactly this. Tell it which interpreter to inspect with the PIPAPI_PYTHON_LOCATION environment variable:

Bash
PIPAPI_PYTHON_LOCATION="$VIRTUAL_ENV/bin/python" pip-audit || echo "pip-audit exit code: $?"

The variable applies to that one command only. This audit needs no package downloads, because the packages are already on disk; it only sends package names and versions to the advisory service.

Check it worked

python app.py prints Hello &lt;Ada&gt; (the angle brackets were escaped by the template). The audit shows the same jinja2 3.1.2 rows as before, and the exit code is 1.

Step 5 · Read the results and compare data sources

An ID on its own tells you little. Ask for the other names each advisory goes by and a description:

Bash
pip-audit -r requirements.txt --aliases on --desc on || echo "pip-audit exit code: $?"

Each row now lists aliases such as a CVE-2024-... number and a GHSA-... ID, followed by a paragraph describing the flaw. The three prefixes come from different databases describing the same problem:

  • PYSEC IDs come from the PyPA Advisory Database, which tracks Python packages specifically.
  • GHSA IDs are GitHub Security Advisories.
  • CVE IDs are the global catalogue most security teams search by.

pip-audit merges results that share any alias, so you see each problem once, and it prefers the PYSEC name.

By default pip-audit asks PyPI, whose JSON API publishes vulnerability data for each release. You can switch to OSV, Google's open vulnerability database that collects the PyPA data alongside other sources:

Bash
pip-audit -r requirements.txt -s osv || echo "pip-audit exit code: $?"

For a well-known package like Jinja2 the two services usually agree. When they differ, the description and the advisory link tell you which versions are affected. For each finding, ask three questions: does the advisory describe a feature you use (the first Jinja2 one concerns the xmlattr filter, for example), what is the fix version, and does upgrading to it break anything?

Check it worked

The first command shows CVE-2024-22195 among the aliases with a description mentioning xmlattr. The OSV run also lists jinja2 3.1.2 and exits with code 1.

Step 6 · Record an accepted risk with a reason

Sometimes you cannot upgrade straight away: no fix exists yet, or the fix needs a release you have not tested. pip-audit's --ignore-vuln hides one advisory by any of its IDs. An ignore with no explanation is a hole nobody remembers, so put the reason, the person who decided and a review date right next to it. A small audit script is a good home:

Bash
cd ~/pip-audit-demo
cat > audit.sh <<'SH'
#!/usr/bin/env bash
# Audit pinned dependencies. Exit code 1 means a known vulnerability was found.
set -euo pipefail

# CVE-2024-22195 (Jinja2 xmlattr filter): our templates never call xmlattr.
# Accepted by: replace with your name, 2026-11-18. Review by: 2026-12-31.
pip-audit -r requirements.txt \
  --ignore-vuln CVE-2024-22195
SH
bash audit.sh || echo "audit exit code: $?"

Use the CVE or GHSA ID in ignores where you can. They are the names other people search for, and pip-audit matches them against aliases.

Check it worked

The summary line now reads Found 4 known vulnerabilities, ignored 1 in 1 package (the count may differ), the table has one row fewer than before, and the exit code is still 1 because the other advisories remain. An ignore removes one known risk from the report; it does not make the rest go away.

Step 7 · Save JSON and CycloneDX reports

The table is for people. For records, dashboards and other tools, write machine-readable files with -f and -o:

Bash
cd ~/pip-audit-demo
pip-audit -r requirements.txt -f json -o audit.json || echo "pip-audit exit code: $?"
python3 -c "import json; d = json.load(open('audit.json')); [print(p['name'], p['version'], v['id'], v['fix_versions']) for p in d['dependencies'] for v in p['vulns']]"

The JSON lists every dependency with its version and a vulns list (empty for clean packages), and each vulnerability has its ID, aliases, description and fix versions. The one-liner prints one line per finding.

CycloneDX is a standard format for a software bill of materials (SBOM): a list of every component in a piece of software. Security teams and some customers ask for one. pip-audit can write it with the vulnerabilities attached:

Bash
pip-audit -r requirements.txt -f cyclonedx-json -o sbom.cdx.json || echo "pip-audit exit code: $?"
python3 -c "import json; b = json.load(open('sbom.cdx.json')); print(b['bomFormat'], b['specVersion'], len(b['components']), 'components,', len(b.get('vulnerabilities', [])), 'vulnerabilities')"

Keep these files with the release they describe, for example as build artifacts, so you can later answer "were we affected on that date?".

Check it worked

The first one-liner prints lines like jinja2 3.1.2 PYSEC-2026-1473 ['3.1.3']. The second prints CycloneDX, a spec version such as 1.4 or newer, a component count of at least 2 and a vulnerability count above 0.

Step 8 · Fix by upgrading, dry run first

pip-audit can edit the requirements file for you. It picks the lowest version that fixes every listed advisory. Look before it touches anything:

Bash
cd ~/pip-audit-demo
pip-audit -r requirements.txt --fix --dry-run || echo "pip-audit exit code: $?"
cat requirements.txt

The dry run prints a line ending in Dry run: would have upgraded jinja2 to 3.1.6 and leaves the file unchanged. When you are happy, run it for real, reinstall the environment from the updated file and check the app still works:

Bash
pip-audit -r requirements.txt --fix
cat requirements.txt
python -m pip install --quiet -r requirements.txt
python app.py
PIPAPI_PYTHON_LOCATION="$VIRTUAL_ENV/bin/python" pip-audit

The fix run exits with code 0 because every vulnerable package was upgraded. In a real project, run your tests between the fix and the commit; a security upgrade can still change behaviour.

The ignore is now stale, so rewrite the audit script without it. Removing stale ignores is part of fixing:

Bash
cat > audit.sh <<'SH'
#!/usr/bin/env bash
# Audit pinned dependencies. Exit code 1 means a known vulnerability was found.
set -euo pipefail

pip-audit -r requirements.txt
SH
bash audit.sh

Check it worked

After the dry run, requirements.txt still says jinja2==3.1.2. After the real fix it says jinja2==3.1.6 (or later, if a newer advisory has appeared since this was written), python app.py still prints Hello &lt;Ada&gt;, and both the environment audit and bash audit.sh print No known vulnerabilities found.

Step 9 · Audit a uv lock file

If your project uses uv, the lock file uv.lock is the record of every exact version, including dependencies of dependencies. pip-audit reads requirements files, so export the lock to one. First, leave the pip environment and turn the requirements into a uv project. --no-sync stops uv from touching .venv:

Bash
cd ~/pip-audit-demo
deactivate
uv init --bare --name pip-audit-demo
uv add --no-sync -r requirements.txt
uv export --format requirements.txt --no-emit-project --frozen -o requirements.lock.txt
head -n 6 requirements.lock.txt

The exported file pins every package and adds --hash=sha256:... lines: fingerprints of the exact files the lock allows. With hashes present, pip-audit can skip pip entirely and read the versions straight from the file:

Bash
pip-audit -r requirements.lock.txt --disable-pip

--disable-pip makes the audit fast and light on data: it downloads no packages, and only sends names and versions to the advisory service. On a metered or prepaid connection, common for developers across much of Africa, that is the difference between a few kilobytes and every wheel your project uses.

Check it worked

head shows a comment saying uv generated the file, followed by lines such as jinja2==3.1.6 \` and `--hash=sha256: lines. The audit prints No known vulnerabilities found and exits with code 0.

Step 10 · Run the audit in CI

A one-off audit is out of date the next day. Run it on every pull request, and on a weekly schedule so new advisories for code you have not touched still reach you. This GitHub Actions workflow exports the lock file fresh and audits it:

Bash
cd ~/pip-audit-demo
mkdir -p .github/workflows
cat > .github/workflows/pip-audit.yml <<'YAML'
name: pip-audit

on:
  pull_request:
  push:
    branches: [main]
  schedule:
    - cron: "17 6 * * 1" # Mondays 06:17 UTC

permissions:
  contents: read

jobs:
  audit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10.4.0
        with:
          version: "0.13.10"
      - name: Export the lock file
        run: uv export --format requirements.txt --no-emit-project --frozen -o requirements.lock.txt
      - name: Audit dependencies
        # Ignores go here as --ignore-vuln <ID>, each with a comment: reason, who, review date.
        run: uvx pip-audit@2.10.1 -r requirements.lock.txt --disable-pip
YAML

The job fails when pip-audit exits 1, which blocks the pull request if you make the check required. Run the two commands from the workflow locally to see what CI will see:

Bash
uv export --format requirements.txt --no-emit-project --frozen -o requirements.lock.txt
uvx pip-audit@2.10.1 -r requirements.lock.txt --disable-pip

Commit pyproject.toml, uv.lock and the workflow. There is no need to commit requirements.lock.txt, because the job regenerates it from the lock each time.

Check it worked

Both local commands succeed and the audit prints No known vulnerabilities found. After you push, the Actions tab of your repository shows a pip-audit run with a green tick. To see it fail on purpose, change jinja2==3.1.6 back to 3.1.2 in a branch with uv add jinja2==3.1.2 and open a pull request: the audit job turns red.

Step 11 · Clean up

Remove the demo project. If you do not want to keep pip-audit, uninstall the tool as well:

Bash
cd ~
rm -rf ~/pip-audit-demo
uv tool uninstall pip-audit

Check it worked

ls ~/pip-audit-demo reports that the folder does not exist, and uv tool list no longer shows pip-audit.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Operational and Technology Management

Get new tutorials

New tutorials, about twice a month.