Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

How do I add a dependency to a group like dev using uv add?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-sonnet-20241022’s answer.
  1. User

    How do I add a dependency to a group like dev using uv add?

  2. Meta Assistant

    A very specific and timely question!

    uv is the new Python package installer from the creators of pip and setuptools, designed to be faster and more efficient. It's still relatively new, but already gaining traction.

    To add a dependency to a specific group (like dev) using uv add, you'll need to use the --group or -G option.

    Here's the syntax:

    uv add <package_name> --group dev
    

    Or, using the shorter flag:

    uv add <package_name> -G dev
    

    Replace <package_name> with the actual name of the package you want to add as a dependency.

    For example, to add pytest as a development dependency, you would run:

    uv add pytest --group dev
    

    or

    uv add pytest -G dev
    

    This will add pytest to the dev group in your pyproject.toml file (which uv uses by default for project configuration).

    If you don't have a pyproject.toml file yet, uv will create one for you with the specified dependency grouped under [project.optional-dependencies], like this:

    [project]
    name = "your_project_name"
    version = "0.1.0"
    
    [project.optional-dependencies]
    dev = [
        "pytest",
    ]
    

    Subsequent uv add commands with the --group dev (or -G dev) flag will append dependencies to the same dev group.

    Some additional tips:

    • You can create multiple groups (e.g., test, docs, lint) and add dependencies to them using the same --group or -G option.
    • To sync your dependencies (i.e., install them) after modifying pyproject.toml, simply run uv sync.
    • If you want to add a specific version of a package, use the usual syntax: uv add 'pytest==6.2.5' --group dev.
    • For more uv add options and details, run uv add --help.

    Happy uv-ing!

  3. User

    And how do I add a package to one of my extras using uv add?

  4. Meta Assistant

    Adding a package to an extra (not to be confused with a group like dev) using uv add is very similar to adding to a group, but with a slightly different flag.

    In pyproject.toml, extras are defined under the [project.optional-dependencies] table. They're used to specify optional dependencies that can be installed using pip's extras syntax, e.g., pip install your_package[extra_name].

    To add a package to an extra using uv add, you'll use the --extra or -E option (note the difference from -G for groups).

    Here's how:

    uv add <package_name> --extra <extra_name>
    

    Or, with the shorter flag:

    uv add <package_name> -E <extra_name>
    

    Let's break it down:

    • <package_name> is the name of the package you want to add.
    • <extra_name> is the name of the extra you want to add the package to.

    Example 1: Adding to an existing extra

    Suppose you already have an api extra defined in your pyproject.toml:

    [project.optional-dependencies]
    api = [
        "requests",
    ]
    

    To add httpx to the api extra, run:

    uv add httpx --extra api
    

    or

    uv add httpx -E api
    

    Your pyproject.toml will now look like this:

    [project.optional-dependencies]
    api = [
        "requests",
        "httpx",
    ]
    

    Example 2: Creating a new extra

    If the extra doesn't exist yet, uv add will create it for you. Let's say you want to add matplotlib to a new plots extra:

    uv add matplotlib --extra plots
    

    This will result in:

    [project.optional-dependencies]
    plots = [
        "matplotlib",
    ]
    

    Key differences from groups (-G):

    • Purpose: Extras (-E) are for optional dependencies that users can choose to install when using your package (e.g., pip install your_package[api]). Groups (-G) are more for internal project management (like separating dev/test dependencies).
    • Usage: Extras are exposed to users of your package; groups are not.
    • Syntax: When installing a package with extras, users do pip install pkg[extra_name]. There's no equivalent user-facing syntax for groups.

    Some additional tips:

    • You can list multiple extras by repeating the --extra/-E flag: uv add package_name -E api -E plots.
    • As with groups, you can specify versions: uv add 'matplotlib==3.5.1' --extra plots.
    • Run uv sync after modifying pyproject.toml to ensure your project's virtual environment reflects the changes.
    • For more details, check uv add --help.

    Now you're all set to manage both groups (-G) for internal project needs and extras (-E) for optional user-facing dependencies with uv!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.