python module_error ai_generated true

ImportError: cannot import name 'HTTPException' from 'fastapi'

ID: python/fastapi-missing-starlette-import

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-08-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

HTTPException is actually from starlette, not fastapi directly in some versions.

generic

中文

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

Workarounds

  1. 95% success Import HTTPException from starlette instead.
    from starlette.exceptions import HTTPException
  2. 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:

  1. Installing an older version of fastapi that has HTTPException. 50% fail

    The import path may have changed; older versions may still have it.

  2. Assuming it's a bug and reinstalling fastapi. 60% fail

    Reinstalling may not fix the import path issue.