Skip to the solution
MediumBackend16 min read

How do uv workspaces and named dependency groups work?

1.2k views

The problem

Question presented to candidate: "Say we have a monorepo with an API service, a worker and a shared core library, all in Python, managed with uv. How would you set it up as a uv workspace, how do the packages depend on each other, and how do you keep test, lint and docs tools out of production installs with dependency groups?"

What a strong answer should cover:

  • A workspace is a root pyproject.toml with [tool.uv.workspace] members (globs) and optional exclude; every member keeps its own pyproject.toml, but there is one uv.lock and one .venv at the root, so all members agree on every version
  • Members depend on each other by normal name in [project].dependencies plus { workspace = true } in [tool.uv.sources]; they are installed as editable, and uv errors if the source entry is missing
  • Commands work on the root by default and target a member with --package (or all with --all-packages); requires-python is the intersection over all members
  • Dependency groups (PEP 735, [dependency-groups]) are named lists of requirements that are not published in the package metadata, unlike extras in [project.optional-dependencies]; { include-group = "..." } composes them, names are normalised, cycles are errors
  • uv installs the dev group by default; --no-dev, --group, --only-group, --all-groups and [tool.uv] default-groups change the selection, and [tool.uv] conflicts allows groups that cannot be installed together
  • Trade-off: one lock means one version of each dependency for the whole repo; members with genuinely incompatible requirements belong in separate projects (or path dependencies)

Clarifying questions expected:

  • Must every service be deployable and versioned separately, or is it one product?
  • Do any members need a different version of a shared dependency, or a different Python?
  • Which tools must be in CI only, and which are needed for local development?

Code / implementation expected: Yes โ€” sketch the root and member pyproject.toml tables and the uv sync / uv run --package commands.

  • uv
  • workspaces
  • monorepo

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

16 min read ยท 13 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonModel of workspace discovery: members, sources, order, Python range
PythonModel of dependency groups: include-group, names, cycles, sync flags

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 38 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextMedium

What is the difference between an iterable and an iterator?

Continue