# ImportError: 无法从部分初始化的模块 'tests' 导入名称 'helpers'（很可能是由于循环导入）

- **ID:** `python/pytest-conftest-import-shadowing`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

嵌套测试目录中的两个 conftest.py 导入同一个 helper 模块，而该模块又从测试包导入，导致 pytest 收集期间形成循环。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   # 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
   ```
2. **** (88% 成功率)
   ```
   # tests/conftest.py
@pytest.fixture
def client():
    from tests.helpers import make_client  # imported at call time
    return make_client()
   ```

## 无效尝试

- **** — Making tests a package can change rootdir behavior and does not break the import cycle between helpers and conftest. (70% 失败率)
- **** — Deferred import still triggers the cycle if helpers imports from tests at module load time. (80% 失败率)
