Configure pytest pythonpath for Monorepo

lesson tools active tools/configure-pytest-pythonpath-fo.md View on GitHub

Configure pytest pythonpath for Monorepo

Rule

Configure pytest pythonpath in pyproject.toml to include all workspace package source directories when working in a monorepo structure.

Context

Pytest runs in its own execution context and cannot automatically discover workspace packages in a monorepo. Without explicit pythonpath configuration, pytest will fail to import local packages even when they are properly structured.

Detection

Pattern

Add pythonpath to pyproject.toml (list all workspace package src directories):

[tool.pytest.ini_options]
pythonpath = [
    "packages/mypkg1/src",
    "packages/mypkg2/src",
    # add one entry per workspace package
]

After adding configuration:

# Tests should now find packages
uv run pytest tests/ -v

Outcome

Related

Match Keywords

pytest ModuleNotFoundError tests fail to import pytest pythonpath workspace package not found cannot import in tests