python module_error ai_generated true

PydanticImportError: `BaseSettings` 已移至 `pydantic-settings` 包。详情见 https://docs.pydantic.dev/2.0/migration/。

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

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-08-05首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

    FastAPI latest requires v2 features; downgrade cascades.

  2. 80% 失败

    Import still fails at class definition time.