Home > Enterprise >  vscode-python extension - "python.envFile"/"envFile" parameters doesn't wor
vscode-python extension - "python.envFile"/"envFile" parameters doesn't wor

Time:11-02

I'm trying to configure a FastAPI based Python project to consume an .env (enviroment variables) file from an alternate location.

I've tried every possible configuration so that VSCode (vscode-python extension) loads the .env file from a specific location. Nothing works. I believe this is a bug of the vscode-python extension.

Any ideas on how I can resolve this (a workaround is also acceptable)?

settings.json (Workspace and User)

{
    [...]
    "python.envFile": "${workspaceFolder}/.env-DEV"
    [...]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            [...]
            "envFile": "${workspaceFolder}/.env-DEV",
            [...]
        }
    ]
}

NOTE: In the enter image description here

  • File structure

    pyenv
    ├─ .env
    └─ Test.py
    
  • Sample code

    from dotenv import load_dotenv
    import os
    load_dotenv()
    print(os.environ.get("envvar"))
    
  • Operation result

    enter image description here

  • Note: If the env file name is not .env. Then you need to specify the file path in the code.

    enter image description here


    EDIT

    launch.json configuration available in debug case

    enter image description here

    • Related