# 导入错误：无法从 'fastapi' 导入名称 'HTTPException'

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

## 根因

在某些版本中，HTTPException实际上来自starlette，而不是直接来自fastapi。

## 版本兼容性

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

## 解决方案

1. **Import HTTPException from starlette instead.** (95% 成功率)
   ```
   from starlette.exceptions import HTTPException
   ```
2. **Use fastapi's HTTPException if available; check version.** (85% 成功率)
   ```
   from fastapi import HTTPException  # Works in recent versions
   ```

## 无效尝试

- **Installing an older version of fastapi that has HTTPException.** — The import path may have changed; older versions may still have it. (50% 失败率)
- **Assuming it's a bug and reinstalling fastapi.** — Reinstalling may not fix the import path issue. (60% 失败率)
