python config_error ai_generated true

pydantic_settings.exceptions.SettingsError: 从源 "EnvSettingsSource" 解析字段 "DATABASE_URL" 的值时出错

pydantic_settings.exceptions.SettingsError: error parsing value for field "DATABASE_URL" from source "EnvSettingsSource"

ID: python/starlette-uvicorn-invalid-env

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

版本兼容性

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

根因分析

Pydantic BaseSettings 字段期望 URL/int/bool,但环境变量格式错误或为空。

English

A Pydantic BaseSettings field expects a URL/int/bool but the environment variable is malformed or empty.

generic

解决方案

  1. 92% 成功率
    from pydantic import PostgresDsn
    class Settings(BaseSettings):
        database_url: PostgresDsn
  2. 95% 成功率
    export DATABASE_URL='postgresql://user:pass@localhost:5432/db'

无效尝试

常见但无效的做法:

  1. 80% 失败

    Downstream code receives None and fails at connect time.

  2. 85% 失败

    Silently connects to the wrong database.