PyCharm - automatically set environment variables

11,906

Solution 1

I was looking for a way to do this today and stumbled across another variation of the same question (linked below) and left my solution there although it seems to be useful for this question as well. They're handling loading the environment variables in the code itself.

Given that this is mainly a problem while in development, I prefer this approach:

  1. Open a terminal

  2. Assuming virtualenvwrapper is being used, activate the virtualenv of the project which will cause the hooks to run and set the environment variables (assuming you're setting them in, say, the postactivate hook)

  3. Launch PyCharm from this command line.

Pycharm will then have access to the environment variables. Likely because of something having to do with the PyCharm process being a child of the shell.

https://stackoverflow.com/a/30374246/4924748

Solution 2

I have same problem. Trying to maintain environment variables through UI is a tedious job. It seems pycharm only load env variables through bash_profile once when it startup. After that, any export or trying to run a before job to change bash_profile is useless

wondering when will pycharm team improve this

In my case, my workaround for remote interpreter works better than local, since I can modify /etc/environment and reboot the vm

for local interpreter, the best solution I can do are these:

1. Create a template Run/Debug config template and clone it

If your env variables are stable, this is a simple solution for creating diff config with same env variables without re-typing them.

  1. create the template config, enter the env variables you need.
  2. clone them

see picture

2. Change your script

Maybe add some code by using os.environ[] = value at your main script but I don't want to do this, it change my product code and might be accidentally committed

Hope someone could give better answer, I've been spent too much time on this issue...

Solution 3

Another hack solution, but a straightforward one that, for my purposes, suffices. Note that while this is particular to Ubuntu (and presumably Mint) linux, there might be something of use for Mac as well.

What I do is add a line to the launch script (pycharm.sh) that sources the needed environment variables (in my case I was running into problems w/ cx_Oracle in Pycharm that weren't otherwise affecting scripts run at command line). If you keep environment variables in a file called, for example, .env_local that's in your home directory, you can add the following line to pycharm.sh: . $HOME/.env_local

Two important things to note here with respect to why I specifically use '.' (rather than 'source') and why I use '$HOME' rather than '~', which in bash are effectively interchangeable. 1) I noticed that pycharm.sh uses the #!/bin/sh, and I realized that in Ubuntu, sh now points to dash (rather than bash). 2) dash, as it turns out, doesn't have the source "builtin", nor will ~ resolve to your home dir.

I also realize that every time I upgrade PyCharm, I'll have to modify the pycharm.sh file, so this isn't ideal. Still beats having to manage the run configurations! Hope it helps.

Share:
11,906

Related videos on Youtube

asafge
Author by

asafge

Updated on June 10, 2022

Comments

  • asafge
    asafge about 2 years

    I'm using virtualenv, virtualenvwrapper and PyCharm. I have a postactivate script that runs an "export" command to apply the environment variables needed for each project, so when I run "workon X", the variables are ready for me.

    However, when working with PyCharm I can't seem to get it to use those variables by running the postactivate file (in the "before launch" setting). I have to manually enter each environment variable in the Run/Debug configuration window.

    Is there any way to automatically set environment variables within PyCharm? Or do I have to do this manually for every new project and variable change?

    • NESPowerGlove
      NESPowerGlove almost 8 years
      asafge, have you found a solution that allows for what you originally were trying to do, by having environment variables get applied through "before launch" scripts, or some other kind of script that PyCharm can run?
    • Colin Nichols
      Colin Nichols over 7 years
      @NESPowerGlove see Antero Ortiz's answer. OP I suggest accepting it as it solves your problem nicely! Just tried it myself.
  • Freddy Tan
    Freddy Tan about 10 years
    btw, if you're interesting in remote interpreter solution. I could post it. However it require more setting. vagrant and virtualbox are required
  • Michal
    Michal almost 9 years
    Just for the future reference. In the second step you shouldn't call your script fabric.py because it will result in a self reference during the first import.
  • Michal
    Michal almost 9 years
    Also the links to the screenshots are dead.