Files
uv/docs/guides/integration/pre-commit.md
T

82 lines
2.1 KiB
Markdown
Raw Normal View History

---
title: Using uv with pre-commit
description:
A guide to using uv with pre-commit to automatically update lock files, export requirements, and
compile requirements files.
---
2024-06-26 12:28:42 -04:00
# Using uv in pre-commit
An official pre-commit hook is provided at
[`astral-sh/uv-pre-commit`](https://github.com/astral-sh/uv-pre-commit).
2024-06-26 12:28:42 -04:00
To use uv with pre-commit, add one of the following examples to the `repos` list in the
`.pre-commit-config.yaml`.
To make sure your `uv.lock` file is up to date even if your `pyproject.toml` file was changed:
```yaml title=".pre-commit-config.yaml"
2025-01-22 05:46:18 +07:00
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
2025-10-17 16:02:02 -05:00
rev: 0.9.4
2025-01-22 05:46:18 +07:00
hooks:
- id: uv-lock
```
To keep a `requirements.txt` file in sync with your `uv.lock` file:
```yaml title=".pre-commit-config.yaml"
2025-01-22 05:46:18 +07:00
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
2025-10-17 16:02:02 -05:00
rev: 0.9.4
2025-01-22 05:46:18 +07:00
hooks:
- id: uv-export
```
To compile requirements files:
2024-06-26 12:28:42 -04:00
2024-07-23 14:29:59 -04:00
```yaml title=".pre-commit-config.yaml"
2025-01-22 05:46:18 +07:00
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
2025-10-17 16:02:02 -05:00
rev: 0.9.4
2025-01-22 05:46:18 +07:00
hooks:
# Compile requirements
- id: pip-compile
args: [requirements.in, -o, requirements.txt]
2024-06-26 12:28:42 -04:00
```
To compile alternative requirements files, modify `args` and `files`:
2024-06-26 12:28:42 -04:00
2024-07-23 14:29:59 -04:00
```yaml title=".pre-commit-config.yaml"
2025-01-22 05:46:18 +07:00
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
2025-10-17 16:02:02 -05:00
rev: 0.9.4
2025-01-22 05:46:18 +07:00
hooks:
# Compile requirements
- id: pip-compile
args: [requirements-dev.in, -o, requirements-dev.txt]
files: ^requirements-dev\.(in|txt)$
2024-06-26 12:28:42 -04:00
```
To run the hook over multiple files at the same time, add additional entries:
2024-06-26 12:28:42 -04:00
2024-07-23 14:29:59 -04:00
```yaml title=".pre-commit-config.yaml"
2025-01-22 05:46:18 +07:00
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
2025-10-17 16:02:02 -05:00
rev: 0.9.4
2025-01-22 05:46:18 +07:00
hooks:
# Compile requirements
- id: pip-compile
name: pip-compile requirements.in
args: [requirements.in, -o, requirements.txt]
- id: pip-compile
name: pip-compile requirements-dev.in
args: [requirements-dev.in, -o, requirements-dev.txt]
files: ^requirements-dev\.(in|txt)$
2024-06-26 12:28:42 -04:00
```