How do I add a dependency to a group like dev using uv add?
- User
How do I add a dependency to a group like dev using uv add?
Assistant
A very specific and timely question!
uvis the new Python package installer from the creators ofpipandsetuptools, 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) usinguv add, you'll need to use the--groupor-Goption.Here's the syntax:
uv add <package_name> --group devOr, using the shorter flag:
uv add <package_name> -G devReplace
<package_name>with the actual name of the package you want to add as a dependency.For example, to add
pytestas a development dependency, you would run:uv add pytest --group devor
uv add pytest -G devThis will add
pytestto thedevgroup in yourpyproject.tomlfile (whichuvuses by default for project configuration).If you don't have a
pyproject.tomlfile yet,uvwill 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 addcommands with the--group dev(or-G dev) flag will append dependencies to the samedevgroup.Some additional tips:
- You can create multiple groups (e.g.,
test,docs,lint) and add dependencies to them using the same--groupor-Goption. - To sync your dependencies (i.e., install them) after modifying
pyproject.toml, simply runuv 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 addoptions and details, runuv add --help.
Happy
uv-ing!- You can create multiple groups (e.g.,
- User
And how do I add a package to one of my extras using uv add?
Assistant
Adding a package to an extra (not to be confused with a group like
dev) usinguv addis 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--extraor-Eoption (note the difference from-Gfor 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
apiextra defined in yourpyproject.toml:[project.optional-dependencies] api = [ "requests", ]To add
httpxto theapiextra, run:uv add httpx --extra apior
uv add httpx -E apiYour
pyproject.tomlwill now look like this:[project.optional-dependencies] api = [ "requests", "httpx", ]Example 2: Creating a new extra
If the extra doesn't exist yet,
uv addwill create it for you. Let's say you want to addmatplotlibto a newplotsextra:uv add matplotlib --extra plotsThis 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/-Eflag: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 syncafter modifyingpyproject.tomlto 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 withuv!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.