python
module_error
ai_generated
true
ModuleNotFoundError: No module named 'myapp'
ID: python/pytest-import-mismatch-module-not-found
80%Fix Rate
90%Confidence
0Evidence
2024-02-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
pytest is run from a directory where the project root is not on sys.path. This often happens when tests are in a subdirectory and the package is not installed, or when using src layout without proper configuration.
generic中文
pytest 在项目根目录不在 sys.path 上的目录中运行。这通常发生在测试位于子目录且包未安装,或使用 src 布局但未正确配置时。
Workarounds
-
90% success
Create conftest.py in the project root (even empty). pytest adds the rootdir to sys.path when it finds conftest.py.
-
98% success
Run: pip install -e . Ensure setup.py or pyproject.toml is present with proper package discovery.
-
95% success
Run: python -m pytest tests/ This adds the current directory to sys.path.
Dead Ends
Common approaches that don't work:
-
70% fail
The current working directory may not be the project root, and this is fragile.
-
80% fail
This makes the parent directory not importable and often breaks relative imports.