pythonrequirements.txtpre-commit.comuv

How to export dependencies to requirement.txt and requirements-dev.txt using uv-pre-commit


I'm using uv as my package manager in my Python project. My pyproject.toml file looks like this:

[project]
name = "some-name"
version = "0.1.0"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    <some-dependencies>
]

[dependency-groups]
dev = [
    <some-dev-dependencies>
]

I'm using uv-pre-commit to export dependencies in a requirement.txt file. But what I really need is to separate general and dev dependencies in separate files. Right now I have this in my .pre-commit-config.yaml:

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

But this would export everything in one single requirement.txt file.

from what I see in the documents, I can do something like this:

    - repo: https://github.com/astral-sh/uv-pre-commit
      # uv version.
      rev: 0.7.3
      hooks:
        # Run the pip compile
        - 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)$

But I'm not sure if this really fits in my case because I don't have *.in files. So I can't figure out how to deal with a pyproject.toml file in uv-pre-commit

Does anyone have an idea how to separate it?


Solution

  • So, the easiest solution would be usage of uv-export hook with dependencies you want to include or exclude.
    In your case, it will be something like:

    repos:
      - repo: https://github.com/astral-sh/uv-pre-commit
        rev: 0.6.17
        hooks:
          - id: uv-export
            name: main dependencies
            args: [--no-dev, --output-file, requirements.txt]
          
          - id: uv-export
            name: dev dependencies
            args: [--only-group, dev, --output-file, requirements-dev.txt]
          
          - id: uv-lock
    

    i think by the name of hooks you will understand what each of them do. Also you dont need .in file for it, since uv is reading directly pyproject.toml