python
module_error
ai_generated
true
ImportError: cannot import name 'helpers' from partially initialized module 'tests' (most likely due to a circular import)
ID: python/pytest-conftest-import-shadowing
80%Fix Rate
82%Confidence
0Evidence
2025-04-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
Two conftest.py files in nested test directories import the same helper module that itself imports from the test package, creating a cycle during pytest collection.
generic中文
嵌套测试目录中的两个 conftest.py 导入同一个 helper 模块,而该模块又从测试包导入,导致 pytest 收集期间形成循环。
Workarounds
-
90% success
# Layout: # tests/conftest.py # tests/helpers.py <-- imports tests.something (cycle) # Fix: move helpers to project root # helpers.py import sys, pathlib sys.path.insert(0, str(pathlib.Path(__file__).parent)) import helpers
-
88% success
# tests/conftest.py @pytest.fixture def client(): from tests.helpers import make_client # imported at call time return make_client()
Dead Ends
Common approaches that don't work:
-
70% fail
Making tests a package can change rootdir behavior and does not break the import cycle between helpers and conftest.
-
80% fail
Deferred import still triggers the cycle if helpers imports from tests at module load time.