Skip to content

Lesson 9 of 9 · 28m

Python Monorepo Layouts That Don't Collapse

Workspace-style Python monorepos: independent distributions, path dependencies, versioning strategy, and CI that tests the blast radius.

Python monorepos fail when teams treat them like one giant Python package. They work when each library remains an independent distribution with explicit boundaries. The monorepo is a source-control and coordination choice, not a license to smear everything across one import namespace.

A layout that scales

platform/
  packages/
    auth-core/          # pyproject.toml + src/auth_core/
    events/             # pyproject.toml + src/events/
    reporting-cli/      # pyproject.toml + src/reporting_cli/
  services/
    api/                # deployable app with its own lockfile
  tools/release.py

Each package has its own distribution name, version, dependencies, tests, and build backend. Services consume libraries through normal dependency declarations. Internal does not mean informal.

Local development uses editable installs

pip install -e packages/auth-core -e packages/events -e services/api

or use a workspace-aware tool such as uv or Poetry where appropriate. The important part is that dependency relationships are written down:

[project]
dependencies = ["auth-core>=1.3,<2", "events>=2.0,<3"]

Do not rely on sys.path hacks. They hide the real dependency graph and make builds irreproducible outside the monorepo checkout.

Version packages independently unless coupling is real

Lockstep versioning is tempting because it looks simple. In practice it creates meaningless major bumps and teaches consumers to ignore versions. Independent versioning works when packages are genuinely separable. Lockstep is justified only when packages ship as one coherent product and internal compatibility is tightly coupled.

CI should test the blast radius, not the universe

If auth-core changes, run its tests, the tests of consumers that declare it, and packaging builds for affected distributions. A dependency graph-driven CI job saves hours compared to running every test on every commit, but scheduled full integration runs still catch interactions path filtering misses.

Release discipline inside the repo

Internal packages can publish to a private index. Tags need namespaces such as auth-core-v1.4.0 and events-v2.1.3; a single v1.4.0 tag cannot represent five independently versioned packages. Release automation should derive package, version, changelog section, and artifact target from the same source of truth.

Field notes

  • Circular internal dependencies are architectural defects; packaging cannot fix them.
  • Keep import package names globally unique in the repo. Two distributions that both provide utils will eventually collide.
  • The root environment is for tooling and integration, not a substitute for each package declaring its own dependencies.
  • If a library is useful outside the monorepo, prove it by installing its wheel in a clean venv.

Was this lesson helpful?

Sign in to rate