You can add .env file under workspace.
.env
SHEETY_ENDPOINT=someting
SHEETY_TOKEN=someting
Then add the following codes to your settings.json:
"python.envFile": "${workspaceFolder}/.env",
Then use shortcuts F5 or Debug Python File so that you can get the environment variable stored in the .env file. You can also use interactive window which can work as well.


Hi everyone,
I'm trying to understand how to use .env files to add extra environment variables to my python programs. Unfortunately, the documentation is quite terse and googling hasn't yielded many useful results.
I've figured a lot of things out, but my current problem is that I simply haven't been able to access the variables in .env within a program itself. I get the feeling that I'm just missing one or two pieces of the puzzle.
I'll try to include everything I think will be relevant.
I've set the IDE up in the following way:
Directory structure
โโ .vscode/ โ โโ launch.json โโ .env โโ testenv.py
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"envFile": "${workspaceFolder}/.env"
}
]
}.env
test_environment_variable=64
testenv.py
import os
print(os.getenv("test_environment_variable"))In the global settings menu, I've also made sure that python.envFile is set how it should be
${workspaceFolder}/.envFinally, I should mention that I'm running python 3.11.3 in VSCode 1.78.2, on a 64-bit Windows 10 PC.
With this configuration, I would expect to print the value of test_envionment_variable, set in .env, when I run testenv.py. However, the only thing it prints is None, meaning of course that it wasn't found. I've also tried printing via os.environ["test_environment_variable"] with similar results (except that it raises KeyError as expected).
What am I missing? Like I mentioned, I suspect I've either got a couple steps wrong, or I'm just entirely misunderstanding how to use .env files.
Thanks in advance!
python - VS Code not recognizing .env file inside workspace folder (.venv) - Stack Overflow
visual studio code - Why I can't access my environment variable from .env file in vscode python? - Stack Overflow
In VS Code-debugger, how do I use envFile in launch.json for nodejs?
Is there any way to set environment variables in Visual Studio Code? - Stack Overflow
Videos
You can add .env file under workspace.
.env
SHEETY_ENDPOINT=someting
SHEETY_TOKEN=someting
Then add the following codes to your settings.json:
"python.envFile": "${workspaceFolder}/.env",
Then use shortcuts F5 or Debug Python File so that you can get the environment variable stored in the .env file. You can also use interactive window which can work as well.


In order for Python to automatically detect a .env file within a workspace folder, you need to ensure that you have the Python extension installed in VS Code. Once you have the extension installed, follow these steps:
- Open the workspace folder that you want to set the environment variables for
- Create a file called
.envin the root of the workspace folder. - Add your environment variables to the
.envfile in the following format:
SHEETY_ENDPOINT=your_value
SHEETY_TOKEN=your_value
- Restart VS Code to apply the changes.
- In your Python code, you can now use os.getenv to retrieve the values of the environment variables. For example:
import os
SHEETY_ENDPOINT = os.getenv("SHEETY_ENDPOINT")
SHEETY_TOKEN = os.getenv("SHEETY_TOKEN")
If you have followed these steps and are still unable to retrieve the environment variable values, then you may need to manually load the environment variables using the dotenv package or by setting them using your operating system's environment variable settings.
I solved my issue but it wasn't after removing the "export" in the .env file (I tried with and without it both gave the same results in the terminal), I had to specify the full path of the .env file in the load_dotenv(), apparently I had to specify it but many code samples I saw in forums didn't need to do it I wonder why?
Here is the new code...
The Python code -

The .env file -

by the way it's not a real private key
At some point before I found the solution I ran the Python script in the terminal and got a random private key (I don't have it pictured sadly), it might had been a key I set in the past but then I checked if I had other .env files in the folder but I had none I also didn't have any environment variables in System Properties > Advanced > Environment Variables on windows so where does dotenv gets the key values by dafault? also After that I reopened vscode and tried again but I got a none error...
(I posted the same answer here: https://stackoverflow.com/a/77337086/12087525)
I've came across this same issue today. Once I undestood that the problem was related to the root directory the dubugger was set, it was straight forward to fix it:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}", // <- the secret is here
"console": "integratedTerminal",
"justMyCode": true
}
]
}
Just add "cwd" as the ${fileDirName} so you set the root directory as the file directory itself, then all the relative paths are going to be fixed as well.
Rederences:
- https://code.visualstudio.com/docs/python/debugging#_python
- https://code.visualstudio.com/docs/editor/variables-reference
You can try this to load the env file.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\Chat\\server.js",
"envFile": "${workspaceFolder}\\Chat\\.env"
}
]
}
I would use the dotenv package to load your .env file, as it can be used by people who aren't using VS Code as well. If you want to include it in your VS Code config, you could do:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "RegressionTestSuite",
"autoAttachChildProcesses": true,
"program": "node -r dotenv/config ${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": []
},
]
}
Your problem could also be that your .env file should not contain export and semi-colons, as it is not a JavaScript/shell file:
SCREEN_SIZE_WIDTH=1366
SCREEN_SIZE_HEIGHT=768
Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
You can load an environment file by setting the envFile property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env", // HERE
"args": [],
"showLog": true
}
]
}
Place the .env file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
By default .env files have a language id of plaintext, but vscode does something special with it to assign a different icon. The only way I've been able to accomplish what you're asking for is with an icons extension.
The dotenv extension adds syntax highlighting and the dotenv language id to all your .env variant files. Pair that with the vscode-icons extensions, and it changes the icon to the gear that the basic .env file has.
With just the icons extension, you can use the properties file association and that works as well, just add the following to settings.json:
"files.associations": {
".env*": "properties"
}
With the dotenv extension this works:
"files.associations": {
"*.env*": "dotenv" // THIS WORKS NOW
}