# ImportError: 尝试相对导入，但没有已知的父包

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

## 根因

测试文件使用了相对导入（例如 from . import module），但它不是包的一部分，或作为脚本运行。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   From the project root, run: python -m pytest tests/
This ensures the current directory is in sys.path and relative imports work if tests are part of a package.
   ```
2. **** (95% 成功率)
   ```
   pip install -e .
Then use absolute imports like 'from mypackage import module' in tests.
   ```

## 无效尝试

- **** — If the test is run as a top-level script, relative imports still fail because there is no parent package. (70% 失败率)
- **** — This may work but can break if the package structure changes or if the test is run from different directories. (60% 失败率)
