python module_error ai_generated true

PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.0/migration/ for more details.

ID: python/fastapi-pydantic-v1-v2-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-08-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Code written for Pydantic v1 imports BaseSettings from pydantic, but Pydantic v2 moved it to a separate package.

generic

中文

为 Pydantic v1 编写的代码从 pydantic 导入 BaseSettings,但 Pydantic v2 将其移至独立包。

Workarounds

  1. 93% success
    pip install pydantic-settings; from pydantic_settings import BaseSettings
  2. 85% success
    Set model_config = SettingsConfigDict(env_file='.env') instead of class Config

Dead Ends

Common approaches that don't work:

  1. 60% fail

    FastAPI latest requires v2 features; downgrade cascades.

  2. 80% fail

    Import still fails at class definition time.