python module_error ai_generated true

pydantic.errors.PydanticUserError: `BaseSettings` 已移至 `pydantic-settings` 包。

pydantic.errors.PydanticUserError: `BaseSettings` has been moved to the `pydantic-settings` package.

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

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

版本兼容性

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

根因分析

项目升级到了 Pydantic v2(FastAPI 0.100+ 要求),但代码仍从 pydantic 导入 BaseSettings。

English

Project upgraded to Pydantic v2 (required by FastAPI 0.100+) but code still imports BaseSettings from pydantic.

generic

解决方案

  1. 95% 成功率
    pip install pydantic-settings
    from pydantic_settings import BaseSettings
  2. 85% 成功率
    class Settings(BaseSettings):
        model_config = SettingsConfigDict(env_file='.env')

无效尝试

常见但无效的做法:

  1. 80% 失败

    Conflicts with FastAPI's dependency requirement and breaks other packages.

  2. 90% 失败

    It's a raised exception, not a warning; import still fails.