Home > Software design >  How do I apply environment variables to Python Interactive (#%%)?
How do I apply environment variables to Python Interactive (#%%)?

Time:09-29

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)?

CodePudding user response:

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 enter image description here

  • Related