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
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.
解决方案
-
93% 成功率
pip install pydantic-settings; from pydantic_settings import BaseSettings
-
85% 成功率
Set model_config = SettingsConfigDict(env_file='.env') instead of class Config
无效尝试
常见但无效的做法:
-
60% 失败
FastAPI latest requires v2 features; downgrade cascades.
-
80% 失败
Import still fails at class definition time.