pip-audit
pip-audit is a command-line tool for scanning Python environments (installed packages, requirements files, or lockfiles) for known vulnerabilities. It leverages various vulnerability databases like OSV and Ecosyste.ms to provide comprehensive security checks. Currently at version 2.10.0, it maintains an active development pace with frequent minor releases to introduce new features, fix bugs, and update dependencies.
Warnings
- breaking The minimum required Python version has progressively increased. As of v2.10.0, Python >=3.10 is required. Earlier versions (v2.8.0 onwards required >=3.9, v2.6.2 onwards required >=3.8) supported older Python versions.
- gotcha Users resolving packages against private package indexes that require authentication might experience hangs. This was a recurring issue with `pip` subprocess invocation.
- gotcha `pip-audit`'s default cache locations on macOS and Linux changed in v2.8.0 to align with platform-specific caching directory idioms (e.g., XDG).
- gotcha On Windows, some versions experienced crashes or issues related to temporary file handling and subprocess deadlocks.
Install
-
pip install pip-audit
Imports
- audit_environment
from pip_audit._api import audit_environment
- audit_requirements
from pip_audit._api import audit_requirements
- main
from pip_audit._cli import main
Quickstart
import subprocess
# Scan the current Python environment
print('Scanning current environment:')
result_env = subprocess.run(['pip-audit', '--output-format', 'json'], capture_output=True, text=True, check=False)
print(result_env.stdout)
# Example: Scan a requirements file (create a dummy one)
with open('requirements.txt', 'w') as f:
f.write('requests==2.25.1\n') # Known vulnerable version
print('\nScanning requirements.txt:')
result_req = subprocess.run(['pip-audit', '-r', 'requirements.txt', '--output-format', 'json'], capture_output=True, text=True, check=False)
print(result_req.stdout)