Sub topic 13
Managing several packages in a workspace
This chapter assumes you can create a uv project, add dependencies, and read a pyproject.toml file. It also assumes you know what uv.lock records. Here you will combine several packages in one repository so they share a single lockfile and a single environment.
What a workspace is
A workspace is a set of packages that live in one repository and are managed together. Each package has its own pyproject.toml, name, and dependencies. The whole workspace shares one uv.lock at the root, so every member is resolved against the same set of versions.
Workspaces fit cases like these:
- An application and the internal libraries it uses.
- Several services that share a common models package.
- A library plus a separate plugin package that you release together.
A workspace has a root, which is the directory with the [tool.uv.workspace] table. The root can itself be a package, as in the example below, or it can be a virtual root that only lists members.
A example layout
The layout below has an application at the root and one library under packages/.
my-workspace/
├── pyproject.toml
├── uv.lock
├── src/
│ └── app/
│ └── __init__.py
└── packages/
└── core-lib/
├── pyproject.toml
└── src/
└── core_lib/
└── __init__.pyThe root pyproject.toml declares the workspace and the dependency on the member.
[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/*"]Three pieces work together here:
[tool.uv.workspace]withmemberstells uv which directories hold member packages. Globs such aspackages/*are allowed. Anexcludelist removes directories that the glob would otherwise match.dependencies = ["core-lib"]is an ordinary dependency, written the same way as for a package from an index.[tool.uv.sources]withworkspace = truetells uv to takecore-libfrom the workspace instead of from PyPI.
Without the sources entry, uv would look for a package called core-lib on the package index. It would either fail or, worse, install an unrelated package with that name.
The member's own file is a normal project file.
[project]
name = "core-lib"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["httpx>=0.27"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"How the pieces connect
The lockfile and the .venv directory both sit at the workspace root. When you run uv sync from any member directory, uv still finds the root and uses the shared lockfile.
Working with a workspace
Create a new member from the root with uv init. It registers the new directory in members if the glob does not already cover it.
uv init --package packages/core-lib
uv add --package app core-lib
uv lock
uv sync
uv run python -c "import core_lib; print(core_lib.__file__)"The flag --package selects which member a command acts on. Without it, uv acts on the member that contains your current directory, or on the root.
The uv sync command installs the root package and its dependencies. Members that the root does not depend on are not installed unless you ask for them. Use these flags to control that:
| Flag | Effect |
|---|---|
| `--package NAME` | Sync or run for one member and its dependencies. |
| `--all-packages` | Install every member of the workspace. |
| `--no-install-workspace` | Install dependencies but not the workspace members themselves. |
For example, uv run --package core-lib pytest runs the tests in the context of core-lib only. That environment contains core-lib and its dependencies, not everything the app needs.
Editable installs
Workspace members are installed in editable mode. Python imports the code straight from the member's source directory. If you edit packages/core-lib/src/core_lib/__init__.py, the next uv run of the app sees the change without a reinstall. This is the main practical benefit over publishing the library and pinning a version.
Shared resolution and its consequences
Because there is one lockfile, uv resolves all members together. This has effects you should know about.
- One version per package. If
appneedshttpx>=0.27and a second member needshttpx<0.26, resolution fails. You cannot give each member its own version of the same dependency in one workspace. - A single
requires-python. uv uses the intersection of every member'srequires-python. A member that supports an older Python lowers what the whole workspace can resolve for. - One lock to review. A dependency change in any member shows up as a diff in the root
uv.lock.
If members truly need conflicting versions, they should not be in the same workspace. Make them separate projects, and reference one from the other with a path source.
Workspace sources versus path sources
Both ways of using a local package live in [tool.uv.sources]. They are not interchangeable.
| Source | Example | Use when |
|---|---|---|
| Workspace | `core-lib = { workspace = true }` | The package is a member of this workspace. |
| Path | `core-lib = { path = "../core-lib" }` | The package is a separate project on disk. |
| Path, editable | `core-lib = { path = "../core-lib", editable = true }` | You want edits to apply without a reinstall. |
A path source points to a project with its own lockfile and its own resolution. A workspace member shares the root's. Prefer a workspace when you control all the packages and release them together.
Common errors
core-libresolves from PyPI, or is not found. Thesourcesentry is missing, or it names a different key than the dependency. The name in[tool.uv.sources]must match the name independencies.- A member is not found. Check that its directory matches a
membersglob, that the directory holds apyproject.toml, and that thenamefield matches what you wrote independencies. ModuleNotFoundErrorfor a member. The member may not be installed in the current environment. Runuv sync --all-packagesoruv sync --package NAME.- Conflicting requirements. Two members demand incompatible versions. Loosen one constraint or split the workspace.
- A member that declares a source. Each member can have its own
[tool.uv.sources]. If one does not set a source, uv uses the root's entry for that package. Putworkspace = truein the root so every member gets the same rule.
When to choose a workspace
Use a workspace when packages change together, share dependency versions, and live in one repository. Choose separate projects when they release on independent schedules, need conflicting dependencies, or have different owners. A workspace gives fast local iteration and a single lock. In return it couples every member to the same resolution.
Exercise
Your workspace has members packages/api and packages/models. api lists models in its dependencies, but uv sync tries to download models from PyPI. What is missing, and where does it go?
Show answer
A source entry that marks models as a workspace member: models = { workspace = true } under [tool.uv.sources]. It can go in the pyproject.toml of api or in the workspace root. The root must also list packages/* (or each directory) under members in [tool.uv.workspace].
Exercise
One member requires httpx>=0.27 and another requires httpx<0.26. What happens when you run uv lock, and what are two ways to resolve it?
Show answer
Resolution fails, because a workspace has one lockfile and one version of each package. Either change one member's constraint so the ranges overlap, or move one member out of the workspace and make it a separate project that is referenced with a path source.