python
module_error
ai_generated
true
import file mismatch: /app/tests/test_models.py has this __file__ attribute: /app/tests/test_models.py which is not the same as the test file we want to collect: /app/tests/unit/test_models.py HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
ID: python/pytest-import-mismatch-missing-init
80%Fix Rate
89%Confidence
0Evidence
2024-02-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 6.x | active | — | — | — |
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
Two test files share the same basename (test_models.py) in different directories, but neither directory is a Python package (no __init__.py), so pytest imports both as top-level module `test_models`, causing a collision.
generic中文
两个测试文件在不同目录下共享相同基名(test_models.py),但两个目录都不是 Python 包(缺少 __init__.py),因此 pytest 将两者都作为顶层模块 `test_models` 导入,导致冲突。
Workarounds
-
95% success
touch tests/__init__.py tests/unit/__init__.py # pytest then imports as tests.test_models and tests.unit.test_models
-
90% success
mv tests/test_models.py tests/test_api_models.py mv tests/unit/test_models.py tests/unit/test_db_models.py
-
85% success
# pytest.ini [pytest] addopts = --import-mode=importlib
Dead Ends
Common approaches that don't work:
-
85% fail
Clearing caches removes stale bytecode but does not fix the underlying module-name collision; the error reappears on the next run.
-
90% fail
Changing PYTHONPATH does not give the two files distinct module names; the collision is due to identical basenames without package context.