【问题标题】:VSCode Python: Pydantic "env_file" overridden by .env file during vscode's Debugging or Testing moduleVSCode Python:在 vscode 的调试或测试模块期间被 .env 文件覆盖的 Pydantic \"env_file\"
【发布时间】:2022-08-16 23:54:44
【问题描述】:

我有两个名为 .env.test.env 的 .env 文件 我正在使用 env_file = \".test.env\" 加载我的 pydantic 设置,如下所示:

from pydantic import BaseSettings

class Settings(BaseSettings):
    A: int

    class Config:
        env_file = \".test.env\"
        env_file_encoding = \"utf-8\"


settings = Settings()

当我从终端运行脚本时,这很好用:

uvicorn run:app

但是,当我使用 VScode 调试或测试时,它会覆盖设置的 env 值.test.env值来自.env

我的启动.json

{
    \"version\": \"0.2.0\",
    \"configurations\": [
        {
            \"name\": \"Python: Module\",
            \"type\": \"python\",
            \"request\": \"launch\",
            \"module\": \"uvicorn\",
            \"args\": [
                \"run:app\",
                \"--reload\"
            ],
            \"justMyCode\": true
        }
    ]
}

如何阻止 VScode 导出.env文件?

    标签: python visual-studio-code pydantic


    【解决方案1】:

    发现 vscode 中有一个名为 Python: Env File 的设置,带有 env 文件的路径。删除路径解决了问题。

    【讨论】:

      猜你喜欢
      • 2022-08-21
      • 2019-05-22
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2021-03-14
      • 2022-01-13
      • 2015-06-30
      • 2022-01-17
      相关资源
      最近更新 更多