Using Dependency Groups with uv
I've slowly been getting more familiar with uv and the broader new school of python packaging ecosystems (sorry, I used virtualenv
for a long time, it does the job fine), and one of the most exciting things to me is its use of dependency groups in a pyproject.toml
file: finally, a non-janky way to use different packages in different environments. However, I think its documentation leaves a bit to be desired on this front, so:
To add a package to a specific group:
uv add --group dev python-dotenv
Which should appear in your pyproject.toml
as:
[dependency-groups]
dev = [
"python-dotenv"=="{version}"```,
]
The part their docs is lacking on is how to actually use these groups when setting up an existing project. It's pretty simple:
To install all packages from all groups:
uv sync
To install packages from a specific group:
uv sync --group {GROUP_NAME}
To exclude a group from your virtualenv:
uv sync --nogroup {GROUP_NAME}
Fun and easy!
Member discussion