You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pydantic v2无法找到SettingsSourceCallable的问题咨询

问题

正在查阅Pydantic 2.4迁移指南,无法找到SettingsSourceCallable的正确导入路径,现有代码因导入语句报错:

from pydantic.env_settings import SettingsSourceCallable

报错信息:

ImportError: cannot import name 'SettingsSourceCallable' from 'pydantic.env_settings' (/usr/local/lib/python3.9/site-packages/pydantic/env_settings.py)
当前依赖配置(requirements.txt)
fastapi~=0.103.2
pydantic~=2.4.2 
uvicorn[standard]~=0.23.2
aiohttp~=3.8.3
PyJWT~=1.7.1
requests~=2.27.1
starlette~=0.27.0
starlette-context~=0.3.6
jose
python-jose-cryptodome
pytest~=7.1.2
pytest-asyncio~=0.19.0
pytest-cov~=3.0.0
aioresponses~=0.7.3
解决方案
  • 安装独立的pydantic-settings包:Pydantic 2.x版本中,BaseSettings及相关类型(包括SettingsSourceCallable)已从核心pydantic包迁移至该独立包,执行安装命令:
    pip install pydantic-settings~=2.4.0
    
  • 修改导入语句为:
    from pydantic_settings import SettingsSourceCallable
    
  • 更新requirements.txt,添加pydantic-settings~=2.4.0条目,保证部署环境同步安装该依赖。

内容的提问来源于stack exchange,提问作者GustaMan9000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 06:00:04