Skip to content

DavidVujic/python-polylith-example-uv

Repository files navigation

Python Polylith Example

This is a repository with an example Python setup of the Polylith Architecture using uv. Here you will find examples of code being shared between different kind of projects, and the developer tooling setup.

Using uv workspaces

Polylith works really well with uv workspaces. In this repo, you'll find an example setup.

The recommended setup for Polylith is to treat the projects as workspaces, instead of having a full-blown pyproject.toml in each component. With this setup, there is no need for any package boilerplate or configuration.

The projects define how the thing should be built, packaged and deployed.

The bricks (components and bases) are the shared code in your Monorepo.

Setup

Define the projects as workspace members in your root pyproject.toml:

[tool.uv.workspace]
members = ["projects/*"]

The project-specific pyproject.toml files defines the needed third-party dependencies. In those, you can specify the dependencies without version if you like. The fixed versions are in they uv.lock file at the repo root.

That's all you need! 🎉

Run a project on your local environment

That's really simple. It's just like running code in a single-project repo.

Example, running the "Greet API" FastAPI endpoints on localhost during development:

uv run uvicorn example.greet_api.core:app --reload

or, by using the FastAPI CLI:

uv run fastapi dev bases/example/greet_api/core.py

Deploying a project

There's several ways to deploy an individual project. In this repo, you will find a setup that should work well for most cases.

Have a look at the "My FastAPI project" README for details.

The main idea is:

  • generate a project-specific lock file (i.e. a requirements.txt)
  • build a wheel for the project, that will contain the needed Polylith bricks only.
  • install the wheel and the dependencies (in the example, Docker is used)

Developer experience

Mypy

Have a look at the mypy.ini configuration file, to make Mypy work really well with this type of architecture.

[mypy]
mypy_path = components, bases
namespace_packages = True
explicit_package_bases = True

Have a look at this repository for more information and documentation: Python tools for the Polylith Architecture

About

Example Polylith setup for Python and uv

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •