1

In Python Interactive using the #%% cell designator for .py files, I frequently use os.getenv("VAR") to test code that uses environment variables. My interpreter is a Python virtual environment. I am frequently adding and removing environment variables to my environment startup script at source $HOME/env/bin/activate like so:

export VAR="value"

To get the variables to work in VS Code Interactive, I have to use Developer: Reload Window which means I lose all my data on the Interactive window. Is there a way apply environment variables to the Python Interactive instance without modifying the env/bin/activate script (which requires a reload)?

j7skov
  • 557
  • 7
  • 22
  • Does this answer your question? [What is the use of python-dotenv?](https://stackoverflow.com/questions/41546883/what-is-the-use-of-python-dotenv) – ErnestBidouille Sep 26 '22 at 18:24
  • if your in the terminal executing commands cant you just restart the virtualenv in the terminal ? if your in a python interpreter already you could just also add it to `os.environ` ala `>>> os.environ["MY_VAR"] = "MY_VAL"` – Joran Beasley Sep 26 '22 at 18:32
  • @ErnestBidouille I cannot install this 3rd party package at work. @JoranBeasley I have a ton of these, I want to source them from the `.env` file. – j7skov Sep 27 '22 at 14:26

2 Answers2

3

The above solution works, but if you change the environment variables in your .env file, you still have to reload the window.

One way to avoid that would be define all the environment variables in the .env file, but that could lead to many-many environment variables.

vs-code recommends to setup two .env files, one for debugging say dev.env and one for prod say prod.env. To add these, in your workspace folder you may see a folder .vscode, it may have two files launch.json and settings.json. launch.json is used for debugging and settings.json is for setting up your workspace settings.

Example of launch.json

{
    // 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": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "python.envFile": "${workspaceFolder}/dev.env"
        }
    ]
}

In settings.json:

{
    "python.envFile": "${workspaceFolder}/prod.env"
}

For launch.json to take effect, you will have to start up your session in your debugging mode.

Again, if you change your environment variables in above mentioned files, you still have to reload the window or restart the kernel for new environment variables to show up, leading you to lose your data on interactive window. If you want to keep your data and still want to change environment variables, you can do so by os.environ[key]=var. If in case you have many variables to add/change, you can as following:

# store your environment variables in json (or config.ini) as key value pair
# following assumes you store your environment variable in json file

import os
import json

env_vars = json.load(open('env_file.json', 'r'))
for key, value in env_vars.items():
    os.environ[key]=value
monte
  • 1,482
  • 1
  • 10
  • 26
1

You can read docs about environment variables.

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.envFile": "${workspaceFolder}/.env" in your user settings . You can change the python.envFile setting at any time to use a different definitions file.

Example:

dev.env:

MYPROJECT_DBUSER=devadmin

test.py:

import os
print(os.getenv('MYPROJECT_DBUSER'))

settings.json:

"python.envFile": "${workspaceFolder}/dev.env"

When I run test.py by using an interactive window, I get

enter image description here

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • That would solve my issue but it does not seem to be working. I set the setting you mentioned and my env vars are still not showing up in Python Interactive (Jupyter). Any idea why? – j7skov Sep 27 '22 at 13:37
  • 1
    I updated my answer. I tried a simple example. You can see what's different between your use and mine. – MingJie-MSFT Sep 28 '22 at 02:16
  • I see my mistake. I had `export ENV_VAR="value"` instead of `ENV_VAR="value"`. Your solution made me realize that, I probably wouldn't have, unless you posted the code. Working as planned, thanks! – j7skov Sep 28 '22 at 13:36
  • Ok, 1 more weird thing. If I use `${workspaceFolder}/.env`, changing the env vars will not get picked up by a restart of the jupyter kernel only. However, if I use an absolute file reference, `/home/ubuntu/git-repo/.env`, it will. Seems like the `${workspaceFolder}` does not pickup changes without a reload. – j7skov Sep 28 '22 at 14:04