Skip to content

Commit 50dce4d

Browse files
authored
Dependency groups and uv run
1 parent c37babb commit 50dce4d

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

uv/dependency-groups.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Dependency groups and uv run
2+
3+
I've adopted a new (to me) pattern for my Python projects to make them easier to hack on using `uv run`. I'm using a [PEP 735 dependency group](https://peps.python.org/pep-0735/) called `dev` to declare my development dependencies - in particular `pytest` - such that running `uv run pytest` executes the tests for my project without me having to even think about setting up a virtual environment first.
4+
5+
Here's the pattern I'm using. I start by creating a new library using `uv init --lib` like this:
6+
7+
```bash
8+
mkdir my-new-library
9+
cd my-new-library
10+
uv init --lib
11+
```
12+
This creates a `pyproject.toml` file like this:
13+
14+
```toml
15+
[project]
16+
name = "my-new-library"
17+
version = "0.1.0"
18+
description = "Add your description here"
19+
readme = "README.md"
20+
authors = [
21+
{ name = "Simon Willison", email = "...@gmail.com" }
22+
]
23+
requires-python = ">=3.10"
24+
dependencies = []
25+
26+
[build-system]
27+
requires = ["uv_build>=0.9.15,<0.10.0"]
28+
build-backend = "uv_build"
29+
```
30+
It also creates a `src/my_new_library/__init__.py` file with a `hello()` example function.
31+
32+
Next, I add `pytest` as a development dependency using this command:
33+
```bash
34+
uv add --dev pytest
35+
```
36+
Doing so adds a new section to the end of that `pyproject.toml` file:
37+
38+
```toml
39+
[dependency-groups]
40+
dev = [
41+
"pytest>=9.0.1",
42+
]
43+
```
44+
This also creates the virtual environment in `.venv` and `uv.lock` file, but we don't need to think about those.
45+
46+
Then I create a test:
47+
48+
```bash
49+
mkdir tests
50+
echo 'from my_new_library import hello
51+
52+
def test_hello():
53+
assert hello() == "Hello from my-new-library!"' > tests/test_my_new_library.py
54+
```
55+
Now I can run that test site using:
56+
```bash
57+
uv run pytest
58+
```
59+
The `dev` dependency group is a special case for `uv run` - it will always install those dependencies as well such that commands like `pytest` can work correctly.
60+
61+
When you package a project for distribution the `dev` dependencies will not be automatically installed for users of your package.
62+
63+
## The importance of [build-system] for specifying a package
64+
65+
That `[build-system]` section is crucial, because it tells `uv` that the directory should be treated as a "package". This means that when `uv run` executes it installs the current directory as an editable package in the virtual environment.
66+
67+
Removing `[build-system]` and then `rm -rf .venv` to delete the virtual environment results in the following error when trying to run `uv run pytest`:
68+
69+
```
70+
________________ ERROR collecting tests/test_my_new_library.py _________________
71+
ImportError while importing test module '/private/tmp/my-new-library/tests/test_my_new_library.py'.
72+
Hint: make sure your test modules/packages have valid Python names.
73+
Traceback:
74+
/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/importlib/__init__.py:126: in import_module
75+
return _bootstrap._gcd_import(name[level:], package, level)
76+
tests/test_my_new_library.py:1: in <module>
77+
from my_new_library import hello
78+
E ModuleNotFoundError: No module named 'my_new_library'
79+
=========================== short test summary info ============================
80+
ERROR tests/test_my_new_library.py
81+
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
82+
=============================== 1 error in 0.07s ===============================
83+
```
84+
Adding the `[build-system]` section back in gets rid of this error.
85+
86+
There's an alternative to having a `[build-system]` section, which is to add this to the `pyproject.toml` file instead:
87+
88+
```toml
89+
[tool.uv]
90+
package = true
91+
```
92+
I [had Claude Code investigate this](https://gistpreview.github.io/?e5405e5770d963ee708eea5ecc769457) and it found [this section of code](https://github.com/astral-sh/uv/blob/d2db06983a20630de4247e4a94af7edd5aa35689/crates/uv-workspace/src/pyproject.rs#L135-L143) explaining what's going on:
93+
94+
```rust
95+
pub fn is_package(&self, require_build_system: bool) -> bool {
96+
// If `tool.uv.package` is set, defer to that explicit setting.
97+
if let Some(is_package) = self.tool_uv_package() {
98+
return is_package;
99+
}
100+
// Otherwise, a project is assumed to be a package if `build-system` is present.
101+
self.build_system.is_some() || !require_build_system
102+
}
103+
```
104+
105+
So there's no need to use that `[tool.uv]` section if you already have a `[build-system]` section.
106+
107+
## Installation in CI
108+
109+
If you're still using regular `pip` in your CI scripts you'll need to ensure the `dev` dependency group is installed like this:
110+
111+
```bash
112+
pip install . --group dev
113+
```
114+
115+
## The end result
116+
117+
Projects that use this pattern become a whole lot easier for other people to hack on. I first used this for my [datasette-extract](https://github.com/datasette/datasette-extract) package, with the result that checking out and running the tests is now a case of running just the following commands:
118+
119+
```bash
120+
git clone https://github.com/datasette/datasette-extract
121+
cd datasette-extract
122+
uv run pytest
123+
```
124+
No need to think about virtual environments or development dependencies - this just works.
125+
126+
Building a distributable wheel of the project is a one-liner as well:
127+
```bash
128+
uv build
129+
```
130+
This creates two files:
131+
```
132+
dist/datasette_extract-0.2a0-py3-none-any.whl
133+
dist/datasette_extract-0.2a0.tar.gz
134+
```
135+
The `.tar.gz` file should contain everything including the tests - the `.whl` file should contain just the non-development Python code.

0 commit comments

Comments
 (0)