python
module_error
ai_generated
true
ImportError: cannot import name 'HTTPException' from 'fastapi'
ID: python/fastapi-missing-starlette-import
80%Fix Rate
85%Confidence
0Evidence
2025-08-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
HTTPException is actually from starlette, not fastapi directly in some versions.
generic中文
在某些版本中,HTTPException实际上来自starlette,而不是直接来自fastapi。
Workarounds
-
95% success Import HTTPException from starlette instead.
from starlette.exceptions import HTTPException
-
85% success Use fastapi's HTTPException if available; check version.
from fastapi import HTTPException # Works in recent versions
Dead Ends
Common approaches that don't work:
-
Installing an older version of fastapi that has HTTPException.
50% fail
The import path may have changed; older versions may still have it.
-
Assuming it's a bug and reinstalling fastapi.
60% fail
Reinstalling may not fix the import path issue.