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
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.
解决方案
-
95% 成功率
pip install pydantic-settings from pydantic_settings import BaseSettings
-
85% 成功率
class Settings(BaseSettings): model_config = SettingsConfigDict(env_file='.env')
无效尝试
常见但无效的做法:
-
80% 失败
Conflicts with FastAPI's dependency requirement and breaks other packages.
-
90% 失败
It's a raised exception, not a warning; import still fails.