python
module_error
ai_generated
true
导入错误:无法从 'fastapi' 导入名称 'HTTPException'
ImportError: cannot import name 'HTTPException' from 'fastapi'
ID: python/fastapi-missing-starlette-import
80%修复率
85%置信度
0证据数
2025-08-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在某些版本中,HTTPException实际上来自starlette,而不是直接来自fastapi。
English
HTTPException is actually from starlette, not fastapi directly in some versions.
解决方案
-
95% 成功率 Import HTTPException from starlette instead.
from starlette.exceptions import HTTPException
-
85% 成功率 Use fastapi's HTTPException if available; check version.
from fastapi import HTTPException # Works in recent versions
无效尝试
常见但无效的做法:
-
Installing an older version of fastapi that has HTTPException.
50% 失败
The import path may have changed; older versions may still have it.
-
Assuming it's a bug and reinstalling fastapi.
60% 失败
Reinstalling may not fix the import path issue.