Python projects often begin as a single application with one pyproject.toml file and one dependency list. As the application grows, teams may split the code into multiple packages, such as an API, shared libraries, background workers, command-line tools, and internal plugins.
Keeping these packages in one repository is convenient, but dependency management can become difficult. Each package may need different dependencies, and developers must ensure that changes in one package do not break another. Maintaining separate virtual environments and lockfiles can also introduce unnecessary complexity.
The uv package manager provides a workspace feature that helps manage multiple related Python packages inside a single repository. A workspace allows packages to maintain their own project configuration while sharing dependency resolution and a common lockfile.
This article explains how to organize a Python monorepo with uv workspaces, connect packages, manage dependencies, run commands, and maintain a reliable development workflow.
What Is a uv Workspace?
A uv workspace is a collection of Python packages managed together. Each package has its own pyproject.toml file, but all workspace members share one uv.lock file.
This approach is useful when multiple packages belong to the same application or product and are developed together.
Consider an application with the following components:
An API service that exposes application endpoints.
A shared library that contains reusable business logic.
A data package that handles database operations.
A worker package that processes background jobs.
Instead of maintaining unrelated dependency configurations, you can organize these packages under one repository and manage them through a single workspace.
A uv workspace provides several benefits:
A shared lockfile keeps dependency resolution consistent across packages.
Each package can declare its own direct dependencies.
Packages can depend on other workspace members.
Workspace packages are installed in editable mode by default.
Developers can run commands for individual packages from the workspace root.
Dependency updates can be managed across the repository.
The important distinction is that a workspace does not remove individual package configurations. Each member still defines its own project metadata and dependencies.
Understanding the Monorepo Structure
Before configuring a workspace, it helps to understand how the repository should be organized.
For this example, create a repository named python-platform with one application and three internal packages.
A possible directory structure is:
python-platform/
│
├── pyproject.toml
├── uv.lock
│
├── apps/
│ └── api/
│ ├── pyproject.toml
│ └── src/
│ └── api/
│ └── main.py
│
└── packages/
├── shared/
│ ├── pyproject.toml
│ └── src/
│ └── shared/
│ └── __init__.py
│
├── database/
│ ├── pyproject.toml
│ └── src/
│ └── database/
│ └── __init__.py
│
└── worker/
├── pyproject.toml
└── src/
└── worker/
└── __init__.py
The repository root contains the workspace configuration and shared lockfile. Every package included as a workspace member has its own pyproject.toml file.
This structure separates application responsibilities while keeping related packages in the same repository.
Configuring the Workspace Root
The first step is to configure the root pyproject.toml file.
The workspace root defines which directories should be treated as workspace members. You can use directory patterns instead of listing every package individually.
For example:
[project]
name = "python-platform"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"fastapi>=0.115",
"uvicorn>=0.30"
]
[tool.uv.workspace]
members = [
"apps/*",
"packages/*"
]
The members field tells uv which directories belong to the workspace. In this example, uv includes packages under apps and packages.
Every directory matched by the member patterns must contain a pyproject.toml file.
The workspace root is also a workspace member. It can define its own project metadata and dependencies, as shown above.
You can exclude directories when they should not participate in workspace management.
For example:
[tool.uv.workspace]
members = [
"apps/*",
"packages/*"
]
exclude = [
"packages/experimental"
]
This configuration includes the application and package directories but excludes the experimental package.
Exclusions are useful when a repository contains examples, prototypes, or projects that should remain outside the shared workspace.
Creating Individual Workspace Packages
Each workspace member should define its own project information and direct dependencies.
For example, the shared package can use the following pyproject.toml file:
[project]
name = "shared"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"pydantic>=2.9"
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
The database package may require SQLAlchemy:
[project]
name = "database"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"sqlalchemy>=2.0",
"psycopg>=3.2"
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
The worker package may need a background task processing library:
[project]
name = "worker"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"celery>=5.4"
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
These configurations keep package responsibilities clear. The database package does not need to declare the worker's dependencies, and the shared package does not need to know which web framework the API uses.
Each package declares the dependencies it directly requires. This makes its configuration easier to understand and maintain.
Connecting Workspace Packages
One of the most useful features of uv workspaces is the ability to connect packages without publishing them to a package registry.
Suppose the API depends on the shared and database packages.
The API package can declare those dependencies in its pyproject.toml file:
[project]
name = "api"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"fastapi>=0.115",
"uvicorn>=0.30",
"shared",
"database"
]
[tool.uv.sources]
shared = { workspace = true }
database = { workspace = true }
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
The dependencies field lists the package names, while tool.uv.sources tells uv where those packages should come from.
The workspace = true setting instructs uv to resolve the dependency from a workspace member instead of downloading it from a package registry.
This distinction is important. Without the workspace source declaration, uv may attempt to resolve a package with that name from an external package index.
Workspace dependencies are installed in editable mode by default. This means that when a developer changes the source code of the shared package, the changes are available to the API without publishing a new package version or reinstalling the package after every edit.
This makes local development much easier when multiple packages are closely connected.
Installing and Synchronizing Dependencies
After configuring the workspace root and its members, run the following command from the repository root:
uv sync
The command resolves dependencies, creates or updates the uv.lock file, and synchronizes the workspace environment.
By default, uv creates a virtual environment named .venv in the workspace root.
The shared lockfile records the resolved dependency versions for the workspace. This helps developers and CI pipelines use consistent dependency versions.
You can also create or update the lockfile explicitly:
uv lock
To synchronize the environment again:
uv sync
The uv run command can also perform locking and synchronization automatically before executing a command.
For example:
uv run python --version
This makes uv convenient for daily development because developers do not need to manually install every dependency after changing project configuration.
Running Commands for Individual Packages
A workspace can contain several applications and libraries, but developers do not always need to run commands from the root package.
uv supports the --package option to target a specific workspace member.
For example, run the API application from the workspace root:
uv run --package api uvicorn api.main:app --reload
Run tests for the shared package:
uv run --package shared pytest
Run a command inside the worker package:
uv run --package worker python -m worker
The --package option is particularly useful in monorepos because developers can execute package-specific commands without changing directories.
It also makes automation easier. A CI pipeline can run tests for individual workspace members while keeping dependency management centralized.
For example, a simple testing workflow might include:
uv run --package shared pytest
uv run --package database pytest
uv run --package api pytest
uv run --package worker pytest
Each command targets a specific package while using the workspace environment.
Managing Development Dependencies
Production dependencies and development tools have different purposes.
An API package may require FastAPI at runtime, while its tests require pytest and its code quality checks require Ruff.
uv supports dependency groups for organizing these requirements.
For example, the API package can define:
[project]
name = "api"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"fastapi>=0.115",
"uvicorn>=0.30"
]
[dependency-groups]
dev = [
"pytest>=8.3",
"ruff>=0.8"
]
The dev group contains tools needed during development rather than application runtime.
You can install the development dependencies with:
uv sync --group dev
To run tests:
uv run --group dev pytest
For a specific workspace member, combine the package and group options:
uv run --package api --group dev pytest
This keeps development tools organized and prevents unnecessary packages from becoming runtime dependencies.
Excluding Packages and Managing Workspace Boundaries
Not every directory in a monorepo should automatically become a workspace member.
A repository may contain documentation examples, temporary experiments, generated code, or deployment scripts. Including every directory can make the workspace configuration harder to maintain.
Use explicit member patterns and exclusions to control the workspace boundary.
For example:
[tool.uv.workspace]
members = [
"apps/*",
"packages/*"
]
exclude = [
"apps/legacy",
"packages/prototype"
]
This keeps the workspace focused on packages that are actively developed together.
It is also important to understand that workspace members share dependency resolution. uv requires a compatible Python version across the workspace. It calculates the intersection of the members' requires-python constraints.
For example, if one member requires Python 3.11 or later and another requires Python 3.12 or later, the workspace must satisfy both constraints.
This is useful for consistency, but it can become a limitation when packages have significantly different Python version requirements.
Workspace Dependencies vs. Path Dependencies
uv supports both workspace dependencies and path dependencies. They solve related problems, but they are not interchangeable in every situation.
A workspace dependency connects a package to another member managed by the same workspace.
A path dependency points to a local package directory and can be used when packages should remain independent projects.
For example, a path dependency can be declared as:
[project]
name = "api"
version = "0.1.0"
dependencies = [
"shared"
]
[tool.uv.sources]
shared = { path = "../../packages/shared" }
This tells uv to resolve shared from a local directory.
The main differences are:
Workspace members share one lockfile and dependency resolution.
Path dependencies can maintain independent project environments and lockfiles.
Workspace members are installed in editable mode by default.
Path dependencies provide more flexibility when packages have different dependency requirements.
The --package option is available for workspace member selection, but not for independent path-based projects in the same way.
Use workspaces when packages are closely connected and should be managed together. Use path dependencies when packages need independent dependency resolution or separate virtual environments.
Using uv Workspaces in CI/CD Pipelines
A consistent local development environment is important, but dependency management must also work reliably in continuous integration pipelines.
The uv.lock file should be committed to source control. This ensures that CI systems and other developers use the same resolved dependency versions.
A basic CI workflow can install dependencies and run tests using:
uv sync --locked
uv run --package shared pytest
uv run --package database pytest
uv run --package api pytest
uv run --package worker pytest
The --locked option checks that the lockfile is up to date with the project configuration. If dependency declarations have changed without updating the lockfile, the command fails instead of silently modifying it.
This is useful in CI because dependency changes should be reviewed and committed intentionally.
For larger repositories, teams can extend the pipeline with formatting, linting, security checks, package builds, and deployment steps.
For Docker-based deployments, workspace configuration requires additional attention. The build process must include the workspace root and the relevant member configuration files. When building production environments, consider using non-editable installations so the resulting environment does not depend on the source tree being present.
Common Mistakes to Avoid
A workspace is straightforward to configure, but several mistakes can cause dependency resolution or development problems.
Missing pyproject.toml Files
Every directory matched by the workspace members patterns must contain a pyproject.toml file. If a matched directory is not a valid project, workspace discovery can fail.
Keep the member patterns specific and exclude directories that are not intended to be packages.
Forgetting workspace = true
Adding an internal package name to the dependencies list does not automatically tell uv to use the local workspace package.
Declare the package source using tool.uv.sources and workspace = true. Otherwise, uv may try to resolve the dependency from a package registry.
Declaring Dependencies in the Wrong Package
Each package should declare the dependencies it directly uses.
For example, if the database package imports SQLAlchemy, SQLAlchemy should be declared in the database package configuration. Relying on another workspace member to install it can hide dependency problems.
Ignoring Python Version Compatibility
Workspace members share a Python version requirement based on the intersection of their requires-python constraints.
Before adding a package, check whether its supported Python versions are compatible with the rest of the workspace.
Not Committing uv.lock
The lockfile is essential for repeatable dependency installation.
If developers update dependencies locally but do not commit the updated lockfile, CI may fail when it checks the project using --locked.
Using a Workspace When Packages Need Isolation
Workspaces are not ideal when members require conflicting dependencies or separate virtual environments.
In those cases, independent projects with path dependencies may provide a better architecture.
Best Practices for Managing Python Monorepos
A well-organized workspace requires more than a correct configuration file. Teams should also establish clear dependency and package ownership rules.
Keep package responsibilities focused. A shared package should contain reusable functionality rather than becoming a place for unrelated application code.
Declare direct dependencies in the package that uses them. This improves maintainability and makes each package easier to understand.
Commit the workspace lockfile and use --locked in CI pipelines. This prevents unexpected dependency changes during automated builds.
Use explicit workspace member patterns. Avoid including temporary directories, generated files, or unrelated examples.
Keep Python version requirements compatible across all members. Review the workspace constraints before introducing packages that support different Python versions.
Finally, use package-specific tests and builds. A shared environment makes dependency management easier, but each package should still be tested according to its own responsibilities.
Conclusion
Managing dependencies across a Python monorepo becomes more challenging as applications grow into multiple packages. Separate environments and lockfiles can create maintenance overhead, while loosely managed local dependencies can introduce inconsistent development experiences.
uv workspaces provide a practical solution by allowing related packages to maintain their own project configurations while sharing a lockfile and dependency resolution.
With workspace member patterns, workspace dependencies, dependency groups, and package-specific commands, teams can organize larger Python codebases without losing control over individual package responsibilities.
The most important decision is choosing the right project structure. Use uv workspaces when packages are developed together and need consistent dependency management. Choose independent projects with path dependencies when packages require separate environments or dependency resolution.
Join the conversation! Your thoughts help the community grow.