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",
[...]
}
]
}
File structure
pyenv
├─ .env
└─ Test.py
Sample code
from dotenv import load_dotenv
import os
load_dotenv()
print(os.environ.get("envvar"))
Operation result
Note: If the env file name is not .env
. Then you need to specify the file path in the code.
EDIT
launch.json configuration available in debug case