Search code examples
pythonuv

How do I install Python dev-dependencies using uv?


I'm trying out uv to manage my Python project's dependencies and virtualenv but I can't see how to install all my dependencies for local development, including the dev dependencies.

In my pyproject.toml I have this kind of thing:

[project]
name = "my-project"
dependencies = [
  "django",
]

[tool.uv]
dev-dependencies = [
  "factory-boy",
]

[tool.uv.pip]
python-version = "3.10"

I can do the following to create a virtualenv and then generate a requirements.txt lockfile, which does not contain dev-dependencies (which is OK, because that is for production):

$ uv venv --python 3.10
$ uv pip compile pyproject.toml -o requirements.txt

But I can't see how to install all the dependencies in my virtualenv.

uv pip sync will use the requirements.txt.

There's also uv sync but I don't understand how that differs, and trying that generates an error:

error: Multiple top-level packages discovered in a flat-layout: ['conf', 'hines', 'docker', 'assets', 'node_modules'].

Solution

  • To expand on the other good answers by Phil

    Dependency Groups

    With uv >= 0.4.27 we can use the new Python packaging feature dependency groups to make pyproject uv-agnostic:

    [project]
    name = "my-project"
    dynamic = ["version"]
    requires-python = ">=3.10"
    dependencies = ["django"]
    
    [dependency-groups]
    dev = ["factory-boy"]
    

    Python Pinned

    Also can make use of .python-version to pin the project Python version, which be created manually or by uv python:

    uv python pin 3.10
    

    Check version written to file

    $ cat .python-version 
    3.10
    

    Dev venv

    With the above configuration in place, a developer virtualenv can be created with:

    uv sync
    

    Finally activate venv:

    source .venv/bin/activate