# 导入错误：起始目录 'tests' 不可导入：'tests' 不是一个包

- **ID:** `python/unittest-testloader-discover-error`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用 unittest.TestLoader().discover('tests') 时，该目录必须是 Python 包（包含 __init__.py），并且可以从当前 Python 路径导入。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.9 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Add an empty __init__.py file to the 'tests' directory to make it a package, then run discovery from the parent directory.
   ```
2. **** (80% 成功率)
   ```
   Use a relative path or run the test script from the parent directory of 'tests' to ensure proper import resolution.
   ```

## 无效尝试

- **** — Adding sys.path.append('.') may fix the import but is a fragile workaround that can cause other import issues. (50% 失败率)
- **** — Changing the discovery pattern (e.g., 'test*.py') without fixing the package structure still fails if the directory is not a package. (60% 失败率)
