skip to Main Content

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.

3

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search