Repository navigation
Use environment variables defined in .env file when running code in a terminal #944
Description
Activity
- addedfeature-requestRequest for new features or functionalityRequest for new features or functionality
on 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!!!
Reacted by Marius, loopinf, Yuriy Shevchenko, Gary, jxltom, Kossak, Gautier Dagan, Tom Runia, Christian Drumm, Gennaro Tedesco and 63 moreHow 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.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.
Dakota Hawkins (@dakotahawkins)
Please could you create an issue for the following: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.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.Reacted by Timothy C. QuinnDakota 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).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.
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
- added a commit that references this issue
on Aug 28, 2023 Should be fixed with #11039, but it is currently behind an experiment. Give it a try:
- Use latest version of VS Code and Python extension
- Add the following to User settings and reload window:
"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
Reacted by James Stidard, Xeraph, Alex, kverburg and Jeff Gruenbaum- added a commit that references this issue
on Sep 12, 2023 If this graduates from being experimental, docs would need to be updated.
- addedverification-neededVerification of issue is requestedVerification of issue is requested
on Sep 19, 2023 starball (@starball5) Thanks, I've added it to our existing issue to track this: microsoft/vscode-docs#6590
- addedverifiedVerification succeededVerification succeededand removedverification-neededVerification of issue is requestedVerification of issue is requested
on Sep 25, 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?Reacted by Jeff GruenbaumThat'll be Remote settings instead of User settings for remote scenarios.
Reacted by GPhiloThank 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:
- Open the command palette (Ctrl+Shift+P)
- click on
Preferences: open user settings (JSON) - Once the JSON file opens, add the following line:
"python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
- save the file and reload your vscode window
Reacted by Sven Fraeys- locked as resolved and limited conversation to collaborators
on Dec 2, 2023
Brett Cannon (@brettcannon)
I've been using
PYTHONPATHvariable in.envfor 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)