Integrate uv into your pre-commit workflow to automatically update lock files, export requirements, and compile dependencies.
Available Hooks
The official astral-sh/uv-pre-commit repository provides three hooks:
- uv-lock: Update
uv.lock when pyproject.toml changes
- uv-export: Export
uv.lock to requirements.txt
- pip-compile: Compile requirements files
Lock File Updates
Automatically update your uv.lock file when pyproject.toml changes:
Add hook to .pre-commit-config.yaml
Export Requirements
Keep a requirements.txt file in sync with your uv.lock file:
This hook will automatically export dependencies whenever the lock file changes.
Custom Export Options
Pass additional arguments to customize the export:
Compile Requirements
Compile requirements.in files to locked requirements.txt files:
Multiple Requirements Files
Compile different requirements files with multiple hook entries:
Alternative File Matching
Customize which files trigger the hook:
Complete Configuration Example
Here’s a comprehensive pre-commit setup using all three hooks:
Version Pinning
Always pin to a specific uv version using the rev field to ensure reproducible builds.
Common Use Cases
Python Project with Lock File
Legacy Project with Requirements Files
Project Exporting to Requirements
Running Hooks Manually
Run all hooks on all files:
Run specific hook:
Run on specific files:
Troubleshooting
Hook Not Running
Ensure pre-commit is installed:
Update Hook Versions
Update to the latest hook versions:
Skip Hooks Temporarily
Skip hooks for a single commit:
Or set the SKIP environment variable: