You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use environment variables defined in .env file when running code in a terminal #944
Brett Cannon (@brettcannon)
I've been using PYTHONPATH variable in .env for debugging using experimental debugger (without having to pip install PTVSD).
However running in terminal doesn't inherit these variables, I've had to export them manually.
What do you think about this feature?
Technical solution outlined in #4310
Need spec to provide UX (prompting users to load env, etc)
The whole "python.envFile" feature needs much better documentation/support (#544)...
I don't know if or how it works, I don't know if I can or how to refer to other variables inside .env (e.g. Does PYTHONPATH=${PYTHONPATH}:${workspaceFolder}/utility work?), etc.
From an hour of trying to use it, it appears nobody on the internet knows what's going on. It's either people who can't get it to work either or people saying it does work but not giving realistic examples... grrr!!!
Reacted by Marius, loopinf, Yuriy Shevchenko, Gary, jxltom, Kossak, Gautier Dagan, Tom Runia, Christian Drumm, Gennaro Tedesco and 63 more
How hard is it to launch with these environment variables set? Does the VS Code API make it easy? If it does then doing it through Create Terminal makes sense.
Dakota Hawkins (@dakotahawkins) and yes, more docs would be good, hence why #544 exists. 😄 If you're up for helping us get that issue closed then it would be appreciated! Else we do plan on getting around to it.
I don't know if or how it works, I don't know if I can or how to refer to other variables inside .env >
(e.g. Does PYTHONPATH=${PYTHONPATH}:${workspaceFolder}/utility work?), etc.
Brett Cannon (@brettcannon) I'd be happy to help out, provided I can figure out how it works or how it's intended to work for more complicated values than FOO=BAR.
Another issue I had is that "run in terminal" didn't work after setting my terminal to bash (git bash, still on windows) because it sent it windows path separators. It occurs to me that these differences might apply to environment variables as well (e.g. separating lists with : instead of ;, path separators from things like ${workspaceFolder} if that's supposed to work, etc.).
In the interim I just went back to cmd.exe. That's probably a separate feature request but I thought I'd mention in here since it could be relevant to processing .env for running in the terminal.
Dakota Hawkins (@dakotahawkins) you're right that the .env file is not terminal-agnostic; you need to write it for your terminal (and we have no explicit support for git bash or WSL at the moment).
At any rate, and more on-topic, one of the things I tried was running a temp wat.py to print values from an environment variable set in .env. It didn't work, and while that helped me find this issue it would be an awesome way to see the effect of variables from .env.
I guess my secondary complaint, besides not knowing how to use it in the first place, is that it seems like there is no way to tell what you're actually getting out of .env.
Transparently setting them when you run in the vscode terminal would have allowed me to do that, but on the other hand I could definitely see it being a big problem for python to work fine from vscode but not anywhere else (because only vscode would be using the .env environment).
I don't know what the "most native" python solution to that would be, but whatever it is should be (imo) as helpful as possible to people who aren't going to run it from inside of vscode.
As I mentioned in #955, my use-case is that I already don't use vscode to run my code and so while that will work I want to be able to get vscode to find all the things my running code would find (without setting absolute paths, because other developers will work on this). The problematic use-case would be the opposite -- some project that starts in vscode becoming dependant on it because of features like this.
Typically people use .env to set their PYTHONPATH environment variable specifically so their code will run in VS Code. Other tools also use it so a .env file isn't special to the extension (we actually didn't come up with the concept and tools like pipenv actually support .env files so it isn't unique to the Python community either).
The setting "python.experiments.optInto": ["pythonTerminalEnvVarActivation"] seems to only be supported in local sessions. Is there perhaps a workaround for remote sessions via SSH?
It's already been mentioned in the above comments, but for those who are trying to implement this and have not messed with experimental settings, here's how to add it:
Activity
dakotahawkins commentedon Mar 5, 2018
The whole
"python.envFile"feature needs much better documentation/support (#544)...I don't know if or how it works, I don't know if I can or how to refer to other variables inside
.env(e.g. DoesPYTHONPATH=${PYTHONPATH}:${workspaceFolder}/utilitywork?), etc.From an hour of trying to use it, it appears nobody on the internet knows what's going on. It's either people who can't get it to work either or people saying it does work but not giving realistic examples... grrr!!!
brettcannon commentedon Mar 5, 2018
How hard is it to launch with these environment variables set? Does the VS Code API make it easy? If it does then doing it through
Create Terminalmakes sense.brettcannon commentedon Mar 5, 2018
Dakota Hawkins (@dakotahawkins) and yes, more docs would be good, hence why #544 exists. 😄 If you're up for helping us get that issue closed then it would be appreciated! Else we do plan on getting around to it.
DonJayamanne commentedon Mar 5, 2018
Dakota Hawkins (@dakotahawkins)
Please could you create an issue for the following:
dakotahawkins commentedon Mar 5, 2018
Don Jayamanne (@DonJayamanne) #955
Brett Cannon (@brettcannon) I'd be happy to help out, provided I can figure out how it works or how it's intended to work for more complicated values than
FOO=BAR.Another issue I had is that "run in terminal" didn't work after setting my terminal to bash (git bash, still on windows) because it sent it windows path separators. It occurs to me that these differences might apply to environment variables as well (e.g. separating lists with
:instead of;, path separators from things like${workspaceFolder}if that's supposed to work, etc.).In the interim I just went back to cmd.exe. That's probably a separate feature request but I thought I'd mention in here since it could be relevant to processing
.envfor running in the terminal.brettcannon commentedon Mar 5, 2018
Dakota Hawkins (@dakotahawkins) you're right that the
.envfile is not terminal-agnostic; you need to write it for your terminal (and we have no explicit support for git bash or WSL at the moment).dakotahawkins commentedon Mar 6, 2018
At any rate, and more on-topic, one of the things I tried was running a temp
wat.pyto print values from an environment variable set in.env. It didn't work, and while that helped me find this issue it would be an awesome way to see the effect of variables from.env.I guess my secondary complaint, besides not knowing how to use it in the first place, is that it seems like there is no way to tell what you're actually getting out of
.env.Transparently setting them when you run in the vscode terminal would have allowed me to do that, but on the other hand I could definitely see it being a big problem for python to work fine from vscode but not anywhere else (because only vscode would be using the
.envenvironment).I don't know what the "most native" python solution to that would be, but whatever it is should be (imo) as helpful as possible to people who aren't going to run it from inside of vscode.
As I mentioned in #955, my use-case is that I already don't use vscode to run my code and so while that will work I want to be able to get vscode to find all the things my running code would find (without setting absolute paths, because other developers will work on this). The problematic use-case would be the opposite -- some project that starts in vscode becoming dependant on it because of features like this.
brettcannon commentedon Mar 6, 2018
Typically people use
.envto set theirPYTHONPATHenvironment variable specifically so their code will run in VS Code. Other tools also use it so a.envfile isn't special to the extension (we actually didn't come up with the concept and tools like pipenv actually support.envfiles so it isn't unique to the Python community either).69 remaining items
Apply custom env variables to terminal when in `pythonTerminalEnvVarA…
karrtikr commentedon Sep 11, 2023
Should be fixed with #11039, but it is currently behind an experiment. Give it a try:
Apply custom env variables to terminal when in `pythonTerminalEnvVarA…
starball5 commentedon Sep 13, 2023
If this graduates from being experimental, docs would need to be updated.
karrtikr commentedon Sep 19, 2023
starball (@starball5) Thanks, I've added it to our existing issue to track this: microsoft/vscode-docs#6590
GPhilo commentedon Oct 11, 2023
The setting
"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]seems to only be supported in local sessions. Is there perhaps a workaround for remote sessions via SSH?karrtikr commentedon Oct 11, 2023
That'll be Remote settings instead of User settings for remote scenarios.
jgrugru commentedon Nov 1, 2023
Thank you for fixing this.
It's already been mentioned in the above comments, but for those who are trying to implement this and have not messed with experimental settings, here's how to add it:
Preferences: open user settings (JSON)"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]