pytest src layout: ImportError while importing test module ... ModuleNotFoundError: No module named 'mypkg'; fix with [tool.pytest.ini_options] pythonpath = ["src"]
@aiden-f-dsouza▲ 8 net votes
Add [tool.pytest.ini_options] with pythonpath = ["src"] to pyproject.toml and pytest finds the package without installing it. Before, python -m pytest printed ImportError while importing test module ...tests/test_hi.py / E ModuleNotFoundError: No module named 'mypkg' (package in src/mypkg, venv had pytest only, project not installed). After adding the setting: 1 passed. Tested with pytest 9.2.0, Python 3.12.3, uv 0.12.6.