如何在Poetry管理的单仓(Monorepo)中复用pyproject.toml配置?
Great question! Managing shared configurations across a Poetry-based monorepo is a common pain point, but there are a few straightforward, maintainable ways to solve it—let’s break down the best approaches:
1. Use Poetry’s Built-In extends for Centralized Parent Configuration
Poetry 1.2+ introduced the extends field, which lets child projects inherit configuration from a parent pyproject.toml file. This is perfect for sharing tooling settings like taskipy, isort, and black.
Step 1: Create a Root Parent Configuration
In your monorepo root, create a pyproject.toml (it doesn’t need to be a full Poetry project—just a container for shared settings):
# monorepo/pyproject.toml [tool.taskipy.tasks] lint = "isort . && black . && flake8" test = "pytest tests/" format = "isort . --fix && black ." [tool.isort] profile = "black" line_length = 88 include_trailing_comma = true multi_line_output = 3 [tool.black] line-length = 88 target-version = ["py310", "py311"] include = '\.pyi?$' # Optional: Share common dev dependencies too [tool.poetry.group.dev.dependencies] pytest = "^7.4.0" isort = "^5.12.0" black = "^23.7.0" taskipy = "^1.13.0" flake8 = "^6.0.0"
Step 2: Inherit from the Parent in Child Projects
In each child project’s pyproject.toml, add the extends field to pull in the root configuration. You can also override or add project-specific settings as needed:
# monorepo/a/pyproject.toml [tool.poetry] name = "a" version = "0.1.0" authors = ["Your Name <you@example.com>"] extends = "../pyproject.toml" # Inherit root config # Project-specific dependencies [tool.poetry.dependencies] python = "^3.10" requests = "^2.31.0" # Optional: Inherit shared dev dependencies + add project-specific ones [tool.poetry.group.dev] inherit = true # Pull in root dev dependencies [tool.poetry.group.dev.dependencies] pytest-cov = "^4.1.0" # Add a dev dep unique to project a # Optional: Override a shared tool setting (e.g., adjust black's line length for this project) [tool.black] line-length = 100
2. Split Shared Configs into a Dedicated File (Alternative)
If you prefer to keep shared tooling settings separate from the root pyproject.toml, create a standalone configuration file (e.g., common-tools.toml) and point child projects to it via extends:
# monorepo/common-tools.toml [tool.taskipy.tasks] lint = "isort . && black ." test = "pytest" [tool.isort] profile = "black"
Then in child projects:
[tool.poetry] extends = "../common-tools.toml"
Key Notes for Success
- Poetry Version: Ensure you’re using Poetry 1.2.0 or newer—older versions don’t support
extendsor dependency group inheritance. - Configuration Merging: Child project settings will override parent settings for any fields you redefine. For tools like
taskipy, child tasks are added to the parent tasks (not replaced). - Consistency: Run commands from the child project directory (e.g.,
cd a && poetry run task lint)—Poetry will resolve the inherited configuration correctly.
These methods eliminate duplicate configs, make updates easier (change once, apply everywhere), and keep your monorepo organized.
内容的提问来源于stack exchange,提问作者northtree

