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.

Answer from MingJie-MSFT on Stack Overflow
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ remote โ€บ advancedcontainers โ€บ environment-variables
Environment variables
November 3, 2021 - version: '3' services: your-service-name-here: env_file: devcontainer.env # ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ using .env files in vs code
r/learnpython on Reddit: Using .env files in VS Code
May 15, 2023 -

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}/.env

Finally, 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!

Discussions

python - VS Code not recognizing .env file inside workspace folder (.venv) - Stack Overflow
In your python code , add the following ...lepath_of_env file>)" ... Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. 2023-08-02T00:45:28.6Z+00:00 ... If you are using the debugger to launch your python app, open the .vscode/launch.json ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
visual studio code - Why I can't access my environment variable from .env file in vscode python? - Stack Overflow
At some point before I found the ... .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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
In VS Code-debugger, how do I use envFile in launch.json for nodejs?
I'm trying to run the debugger in VS Code on my nodejs application. I'm using an .env file to store environment variables that I later call with process.env.. When I looked up the VS Code docs fo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there any way to set environment variables in Visual Studio Code? - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-python โ€บ issues โ€บ 544
Document how .env files work ยท Issue #544 ยท microsoft/vscode-python
January 8, 2018 - Should probably cover it in https://code.visualstudio.com/docs/python/environments. Came up due to confusion found at https://stackoverflow.com/questions/48120642/visual-studio-code-python-import-paths .
Author ย  brettcannon
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ reference โ€บ variables-reference
Variables reference
November 3, 2021 - For example, ${env:USERNAME} references the USERNAME environment variable.
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ environments
Python environments in VS Code
November 3, 2021 - The selected environment is used whenever you run or debug files in that project. Click the environment to change it. When you assign an environment to a project, the extension writes to your workspace settings (.vscode/settings.json):
Top answer
1 of 5
9

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...

2 of 5
3

(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
Find elsewhere
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
ENV - Visual Studio Marketplace
October 13, 2020 - Extension for Visual Studio Code - Adds formatting and syntax highlighting support for env files (.env) to Visual Studio Code
๐ŸŒ
GitHub
github.com โ€บ IronGeek โ€บ vscode-env
GitHub - IronGeek/vscode-env: .ENV formatter for Visual Studio Code ยท GitHub
Adds formatting and syntax highlighting support for env files (.env) to Visual Studio Code
Starred by 34 users
Forked by 2 users
Languages ย  TypeScript
๐ŸŒ
DEV Community
dev.to โ€บ nausaf โ€บ use-env-files-for-storing-development-secrets-and-configuration-for-net-core-projects-in-vs-code-1kbh
Use .env files for .NET local development configuration with VS Code - DEV Community
July 14, 2025 - All of your local development-time secrets and other configuration - for all projects and not just .NET if you have multiple ecosystems - are now in the .env files located in a single folder: in .vscode.
๐ŸŒ
DEV Community
dev.to โ€บ andreasbergstrom โ€บ placeholder-post-1klo
How to make VS Code read dotenv file when debugging - DEV Community
September 24, 2020 - { "version": "0.2.0", "configurations": [ { "type": "node", "stopOnEntry": false, "envFile": "${workspaceFolder}/.env", "request": "launch", "name": "Launch Program", "skipFiles": ["<node_internals>/**"], "program": "${workspaceFolder}/src/app.js" } ] }
Top answer
1 of 15
123

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}"
    }
  ]
}

2 of 15
73

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

๐ŸŒ
Medium
krishprabha2021.medium.com โ€บ how-to-create-a-env-file-in-vscode-even-if-touch-isnt-recognized-207819fa02c0
How to Create a .env File in VSCode (Even if touch Isnโ€™t Recognized) | by Krishnaveni | Medium
October 6, 2025 - ๐Ÿ’ก Pro Tip: If VSCode automatically adds .txt (creating .env.txt), rename it and remove the .txt extension. Your file name must be just .env โ€” nothing else.
๐ŸŒ
Kastroud Notes
kastroud.hashnode.dev โ€บ adding-env-variables-to-your-spring-boot-project-on-vs-code
Adding Env variables to your spring-boot project on VS Code.
September 29, 2024 - This can save developers a significant amount of time and effort compared to manually configuring environment variables outside of the IDE. To setup VS Code for using Java and Spring Boot have a look at these links ... Create a new workspace configuration by opening run -> add configuration. This will create a launch.json file in the .vscode folder which you can use to configure the environment that the java application is launched in.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ javascript-by-example โ€บ 9781788293969 โ€บ d34ba441-abb3-4937-acf1-a2e7d54ffb23.xhtml
Creating .env file in Windows - JavaScript by Example [Book]
August 30, 2017 - Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name .env ...
Author ย  Dani Akash S
Published ย  2017
Pages ย  298
๐ŸŒ
Medium
medium.com โ€บ @viniciu_ โ€บ automatically-loading-the-env-file-in-your-vs-code-embed-terminal-30b2b32447f7
Automatically Loading the .env File in Your VS Code Embed Terminal - Vinicius - Medium
June 20, 2023 - 1 โ€” Open the file ~/.zprofile (create it if it doesn't exist); 2 โ€” Add the following code: # Loading `.env` on VS Code embeded terminal if [[ "$TERM_PROGRAM" == "vscode" && -f ".env" ]]; then source .env && \ echo "โœ… loaded .env" fi ยท 3 โ€” Save the file; 4 โ€” Close and reopen your embedded terminal in VS Code; 5 โ€” Voilร !
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-docs โ€บ issues โ€บ 6967
Explain how to disable autoloading of .env file in Python environment docs ยท Issue #6967 ยท microsoft/vscode-docs
January 19, 2024 - The Python environments in VS Code docs say By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions. The file is identified by the default entry "python.envFi...
Author ย  gmeligio