VS Code not recognizing .env file inside workspace folder (.venv)

4.7k Views Asked by At

I'm trying to set environment variables within a Python virtual environment in VS Code (mostly for API keys). The VS Code documentation here: https://code.visualstudio.com/docs/python/environments suggests that Python will automatically detect a .env file within a workspace folder. However, that doesn't seem to be happening. When I enter the following code, the terminal returns a value of None.

import os
SHEETY_ENDPOINT = os.getenv("SHEETY_ENDPOINT")
SHEETY_TOKEN = os.getenv("SHEETY_TOKEN")

I'm using the dotenv package to make the code work right now, but don't want to have to rely on it if it is unnecessary in a VS Code workspace.

4

There are 4 best solutions below

0
Hack-Z On

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:

  1. Open the workspace folder that you want to set the environment variables for
  2. Create a file called .env in the root of the workspace folder.
  3. Add your environment variables to the .env file in the following format:
SHEETY_ENDPOINT=your_value
SHEETY_TOKEN=your_value
  1. Restart VS Code to apply the changes.
  2. 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.

0
Tom Chen On

If you are using the debugger to launch your python app, open the .vscode/launch.json at the project root, edit the envs in env, e.g.

{
        "name": "Python: API Server",
        "type": "python",
        "request": "launch",
        "module": "uvicorn",
        "args": [
            "app.main:app",
            "--port",
            "8000",
            "--host",
            "0.0.0.0"
        ],
        "env": {                
            "PORT": "8000"                
        }
    }
}

ref: https://code.visualstudio.com/docs/python/debugging

1
MingJie-MSFT On

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.

enter image description here

enter image description here

1
Akanksha On

Loading Environment Variables in VSCode

  1. Create a .env file in your root directory. Place all your environment variables in the .env file. <var_name>=value
  2. In your VS Code, go to settings and then python extension . Update the path of .env in the python extension.
  3. Restart VS code
  4. pip install python-dotenv
  5. In your python code , add the following lines to load the variables from .env file: from dotenv import load_dotenv load_dotenv(<filepath_of_env file>)"