uv/docs/guides/integration/pre-commit.md
Zanie Blue 1e8e08def2 Bump version to 0.7.0 and write changelog (#13201)
The changelog diff is deranged. Rendered at
https://github.com/astral-sh/uv/blob/zb/changelog-07/CHANGELOG.md#070

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2025-04-29 16:37:00 -05:00

2.1 KiB

title description
Using uv with pre-commit A guide to using uv with pre-commit to automatically update lock files, export requirements, and compile requirements files.

Using uv in pre-commit

An official pre-commit hook is provided at astral-sh/uv-pre-commit.

To make sure your uv.lock file is up to date even if your pyproject.toml file was changed via pre-commit, add the following to the .pre-commit-config.yaml:

repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.7.0
    hooks:
      - id: uv-lock

To keep your requirements.txt file updated using pre-commit:

repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.7.0
    hooks:
      - id: uv-export

To compile requirements via pre-commit, add the following to the .pre-commit-config.yaml:

repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.7.0
    hooks:
      # Compile requirements
      - id: pip-compile
        args: [requirements.in, -o, requirements.txt]

To compile alternative files, modify args and files:

repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.7.0
    hooks:
      # Compile requirements
      - id: pip-compile
        args: [requirements-dev.in, -o, requirements-dev.txt]
        files: ^requirements-dev\.(in|txt)$

To run the hook over multiple files at the same time:

repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.7.0
    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)$