tox-uv

repository·main·Indexed 43 days ago

https://github.com/tox-dev/tox-uv

A tox plugin that replaces default virtualenv and pip creation and management with the high-performance uv tool. It supports uv.lock synchronization, PEP 723/735, and provides specialized runners like uv-venv-runner and uv-venv-lock-runner to leverage uv's speed and resolution strategies.

Tokens
2K
Snippets
8
Records
13
Agent score
6%

What's inside tox-uv

  1. How cache invalidation works for UV_* environment variables

    main

    To ensure that changes to uv configuration are reflected in your environments, tox-uv includes a specific set of UV_* environment variables in its install cache key.

    If you modify any of the tracked variables using set_env in your tox.ini, tox-uv will detect the change, invalidate the existing cache for that environment, and trigger a re-installation/re-resolution.

    Tracked variables (trigger re-install): UV_CONSTRAINT, UV_DEFAULT_INDEX, UV_EXCLUDE, UV_EXCLUDE_NEWER, UV_EXTRA_INDEX_URL, UV_FIND_LINKS, UV_FORK_STRATEGY, UV_INDEX, UV_INDEX_STRATEGY, UV_INDEX_URL, UV_KEYRING_PROVIDER, UV_NO_INDEX, UV_NO_SOURCES, UV_OFFLINE, UV_OVERRIDE, UV_PRERELEASE, UV_REQUIRE_HASHES, UV_RESOLUTION, UV_TORCH_BACKEND.

    Untracked variables (do NOT trigger re-install): Variables that do not affect dependency resolution, such as UV_CONCURRENT_DOWNLOADS, UV_NO_PROGRESS, or UV_CACHE_DIR, are not tracked. Changing these will not cause tox-uv to reinstall packages.

  2. Configure uv.lock support with uv-venv-lock-runner

    main

    To use uv sync with a uv.lock file, set the runner to uv-venv-lock-runner. In this mode, all dependencies are managed via the lock file, and the deps configuration option is ignored. You must use the extras option to specify which dependency groups to install.

    Example tox.ini configuration:

    [testenv:type]
    runner = uv-venv-lock-runner
    description = run type checker via mypy
    commands = mypy {posargs:src}
    
    [testenv:dev]
    runner = uv-venv-lock-runner
    description = dev environment
    extras =
        dev
        test
        type
    commands =
        uv pip tree
    [testenv:type]
    runner = uv-venv-lock-runner
    description = run type checker via mypy
    commands =
        mypy {posargs:src}
    
    [testenv:dev]
    runner = uv-venv-lock-runner
    description = dev environment
    extras =
        dev
        test
        type
    commands =
        uv pip tree
  3. Choose between tox-uv and tox-uv-bare

    main

    Depending on your environment, choose one of the two distribution packages:

    1. tox-uv (Recommended): A meta package that includes both the plugin and a bundled uv binary. This is best for local development to ensure uv is always available.
    2. tox-uv-bare: A plugin-only package without the bundled uv binary. Use this in containerized environments (Docker, Kubernetes) where uv is already pre-installed in the system to avoid redundant downloads.
    FROM python:3.12
    RUN pip install uv tox tox-uv-bare
    # uv is already in the container, no need to bundle it again
  4. Install and use tox-uv

    main

    To use tox-uv, install it into your tox environment. Once installed, it replaces virtualenv and pip with uv for all runs. You can verify the installation with tox --version and run environments using the uv backend by running tox r -e <env_id>.

    # Use uv to install tox with the tox-uv plugin
    uv tool install tox --with tox-uv
    
    # Validate installation
    tox --version
    
    # Run an environment using uv
    tox r -e py312
    
    # Explicitly fallback to virtualenv+pip if needed
    tox --runner virtualenv r -e py312
  5. Run PEP 723 inline scripts with tox-uv

    main

    PEP 723 allows Python scripts to declare dependencies inline. When tox-uv is installed, the virtualenv-pep-723 runner is automatically backed by uv. You can also explicitly use runner = uv-venv-pep-723 in your tox.ini.

    To disable this automatic behavior and use the standard virtualenv+pip implementation, set the environment variable TOX_UV_NO_PEP723=1.

    # tools/check.py content:
    # /// script
    # requires-python = ">=3.12"
    # dependencies = ["requests>=2.31", "rich"]
    # ///
    
    [testenv:check]
    runner = virtualenv-pep-723
    script = tools/check.py
  6. Configure uv sync dependency groups

    main

    When using uv-venv-lock-runner, you can control which dependency groups are installed using the following keys:

    • extras: A list of strings selecting which extra groups to install with uv sync (e.g., extras = ['dev', 'test']).
    • dependency_groups: Specifies PEP 735 – Dependency Groups to install in addition to the project and its dependencies (maps to uv sync --group).
    • only_groups: Installs only these groups, excluding the project and all other dependencies (maps to uv sync --only-group). Use this for isolated CI tooling.
  7. Configure package installation modes for uv-venv-runner

    main

    When using uv-venv-runner, you can control how the source tree package is installed via the package key. Options include:

    • skip: Do not install the project.
    • wheel: Install as a non-editable wheel.
    • editable (default): Install in editable mode.
    • uv: Uses uv directly to install the project (bypassing tox's PEP-517 packaging).
    • uv-editable: Uses uv directly to install in editable mode.
  8. Configure uv binary discovery via TOX_UV_PATH

    main

    If uv is installed in a non-standard location or you are testing a custom build, you can explicitly specify the uv binary location using the TOX_UV_PATH environment variable. Otherwise, tox-uv looks for the bundled uv (in the tox-uv package) or searches your system PATH (in tox-uv-bare).

    export TOX_UV_PATH=/custom/path/to/uv
    tox r
  9. Configure uv sync flags and locking behavior

    main

    Fine-tune the uv sync command using these configuration keys:

    • uv_sync_flags: A list of strings containing additional flags passed to uv sync (e.g., uv_sync_flags = ['--no-editable', '--inexact']).
    • uv_sync_locked: A boolean. If true (default), tox-uv calls uv sync --locked. Set to false to allow other flags like --upgrade to work without conflict.
    • uv_seed: A boolean (environment level) that controls if pip, setuptools, and wheel are injected into the virtual environment. Default is off.
  10. Configure uv resolution strategies in tox

    main

    You can control how uv selects package versions for a tox environment using the uv_resolution option. This is useful for testing the lower bounds of your dependencies by forcing uv to pick older compatible versions instead of the latest ones.

    Available strategies:

    • highest (default): Selects the highest version satisfying constraints.
    • lowest: Installs the lowest compatible versions for all dependencies (both direct and transitive).
    • lowest-direct: Installs the lowest compatible versions for direct dependencies, but uses the latest compatible versions for transitive dependencies.

    Note: When using uv_resolution alongside dependency_groups, tox-uv combines all dependencies from deps and dependency_groups into a single installation operation to ensure the resolution strategy is applied consistently across all requirements.

    [testenv]
    uv_resolution = "lowest"
  11. Disable PEP 723 support via environment variable

    main

    By default, tox-uv provides a virtualenv-pep-723 environment type. If you need to prevent this environment type from being registered, set the TOX_UV_NO_PEP723 environment variable to true.

    export TOX_UV_NO_PEP723=true
    tox run
  12. Use tox-uv runners for different environment types

    main

    The plugin provides several specialized runner IDs for different use cases:

    • uv-venv-runner: For environments not using a lock file (uses uv pip).
    • uv-venv-lock-runner: For environments using uv.lock (uses uv sync).
    • uv-venv-pep-723: For PEP 723 inline script metadata. Note that virtualenv-pep-723 is also transparently backed by uv if the plugin is installed.
    • uv-venv-pep-517: For PEP-517 packaging environments.
    • uv-venv-cmd-builder: For external command builders.