Sub topic 13
Managing several packages in a workspace
This chapter assumes you can create a project with uv init, add dependencies with uv add, and read [project] and [build-system] tables. It also assumes you know what uv.lock records.
What a workspace is
A uv workspace is a set of packages that live in one repository and share a single lockfile. Use one when an application depends on a library you are developing next to it. You edit the library and the application sees the change immediately, with no publishing step.
The root pyproject.toml declares the workspace. Each member is a directory with its own pyproject.toml.
Declaring the workspace
The root project lists its members under [tool.uv.workspace]. It points at the member through [tool.uv.sources]:
[project]
name = "app"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["core-lib"]
[tool.uv.sources]
core-lib = { workspace = true }
[tool.uv.workspace]
members = ["packages/*"]The workspace = true entry tells uv to take core-lib from the workspace and not from PyPI. Without it, uv looks for a published package with that name.
Creating and using a member
Create the member from the root directory:
uv init --lib packages/core-lib
uv sync
uv run python -c "import core_lib"uv init --lib makes a package with a src layout and a build backend. Because the parent directory is a workspace, uv also registers the new package as a member.
uv sync installs the root project and every member into one .venv. Workspace members are installed in editable mode, so source changes take effect without reinstalling.
Targeting one package
Commands run against the root project by default. Use --package to pick a member:
uv add --package core-lib httpx
uv run --package core-lib pytest
uv sync --package core-libuv add --package core-lib httpx writes the dependency into the member's own pyproject.toml. The lockfile still stays in the workspace root.
Rules to remember
- There is one
uv.lockfor the whole workspace. Every member is resolved together. - Every member shares one environment, so their dependencies must be compatible. Two members that need conflicting versions of the same library cannot live in one workspace.
- uv picks a
requires-pythonthat satisfies all members. A member with a narrower range narrows it for the entire workspace. - Sources in the root's
[tool.uv.sources]apply to every member unless a member overrides them.
Exercise
You run uv add requests inside packages/core-lib and later uv lock in the repository root. How many lockfiles exist, and which pyproject.toml lists requests?
Show answer
One lockfile exists, uv.lock in the workspace root, covering every member. requests is listed in packages/core-lib/pyproject.toml, because uv add edits the pyproject of the project you run it in (or the one named by --package).