Skip to content

Use environment variables defined in .env file when running code in a terminal #944

Description

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)

Activity

  1. dakotahawkins commented on Mar 5, 2018

    @dakotahawkins

    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!!!

  2. brettcannon commented on Mar 5, 2018

    @brettcannon
    Member

    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.

  3. brettcannon commented on Mar 5, 2018

    @brettcannon
    Member

    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.

  4. DonJayamanne commented on Mar 5, 2018

    @DonJayamanne
    Author

    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.

  5. dakotahawkins commented on Mar 5, 2018

    @dakotahawkins

    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 .env for running in the terminal.

  6. brettcannon commented on Mar 5, 2018

    @brettcannon
    Member

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

  7. dakotahawkins commented on Mar 6, 2018

    @dakotahawkins

    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.

  8. brettcannon commented on Mar 6, 2018

    @brettcannon
    Member

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

  9. 69 remaining items

  10. karrtikr commented on Sep 11, 2023

    @karrtikr

    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"]
  11. starball5 commented on Sep 13, 2023

    @starball5

    If this graduates from being experimental, docs would need to be updated.

  12. karrtikr commented on Sep 19, 2023

    @karrtikr

    starball (@starball5) Thanks, I've added it to our existing issue to track this: microsoft/vscode-docs#6590

  13. GPhilo commented on Oct 11, 2023

    @GPhilo

    The setting "python.experiments.optInto": ["pythonTerminalEnvVarActivation"] seems to only be supported in local sessions. Is there perhaps a workaround for remote sessions via SSH?

  14. karrtikr commented on Oct 11, 2023

    @karrtikr

    That'll be Remote settings instead of User settings for remote scenarios.

  15. jgrugru commented on Nov 1, 2023

    @jgrugru

    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:

    • 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
  16. locked as resolved and limited conversation to collaborators on Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-environmentsFeatures relating to handling interpreter environmentsarea-terminalfeature-requestRequest for new features or functionalityverifiedVerification succeeded

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions