Setting up a Python monorepo to build multiple packages using UV (the fast Python package manager) is a great way to manage related packages in a single repository. As of mid-2024, UV supports PEP 621 (pyproject.toml-based) projects and can work with monorepos, though some features (like workspace management) are still evolving.
Here’s a step-by-step guide to set up a Python monorepo with multiple packages using UV:
Per-package:
Navigate to each package and use UV to install dependencies:
bash
cd packages/package_a
uv pip install -e .
This will install the package in editable mode and resolve dependencies.
Note:
UV does not (as of June 2024) have a built-in "workspace" or "multi-project" install command like pnpm or yarn in JS, but you can script this easily.
5. Developing Locally with Interdependencies
If package_b depends on package_a, add a local path dependency in package_b/pyproject.toml:
toml
[project]
name = "package_b"
version = "0.1.0"
dependencies = [
"package_a @ file://../package_a"
]
Then, from package_b:
bash
uv pip install -e .
This will install package_a from the local path.
6. Scripting Monorepo Tasks
You can automate installs for all packages with a simple shell script:
bash
for pkg in packages/*; do
(cd "$pkg" && uv pip install -e .)
done
7. Building Packages
To build a package (wheel/sdist):
bash
cd packages/package_a
uv build
This will create dist/ with the built artifacts.
8. (Optional) Top-level Tooling
If you want a top-level pyproject.toml for shared dev dependencies or tools (like ruff, pytest, etc.), you can add one at the repo root, but keep in mind that UV will not automatically manage all sub-packages as a workspace (yet).
9. Summary
Each package is a PEP 621 project with its own pyproject.toml.
Use uv pip install -e . in each package for development.
Use local path dependencies for inter-package links.
If you need workspace-like features (single lockfile, cross-package dependency resolution), keep an eye on UV’s roadmap or consider tools like PDM or Poetry which have more mature monorepo support as of now.