42

When I use vim to update my environmental variables (in ~/.bashrc), PyCharm does not get the updates right away. I have to shut down the program, source ~/.bashrc again, and re-open PyCharm.

Is there any way to have PyCharm source the changes automatically (or without shutting down)?

alex
  • 6,818
  • 9
  • 52
  • 103
RenaissanceProgrammer
  • 404
  • 1
  • 11
  • 30
  • Curious... how often do you update environment vars? Also, you might have better luck at http://superuser.com. – isherwood Dec 01 '15 at 20:10
  • not very often, some sensitive information for my project is stored in environmental vars rather than in the code, i see the variables correctly when i do `$ printenv` so i don't think it's an issue with the OS – RenaissanceProgrammer Dec 01 '15 at 20:12
  • Would the problem be solved by reloading the interpreter as described here : https://www.jetbrains.com/pycharm/help/installing-uninstalling-and-reloading-interpreter-paths.html#d112620e166 ? – Mr_and_Mrs_D Dec 05 '15 at 11:40
  • @Mr_and_Mrs_D tried that, it took 4 menus to get to the reloading option and it didn't work, looks like what omer727 below says is correct, the app just won't get updated vars because it's a child process started before the changes were made – RenaissanceProgrammer Dec 08 '15 at 20:19

9 Answers9

26

When any process get created it inherit the environment variables from it's parent process (the O.S. itself in your case). if you change the environment variables at the parent level, the child process is not aware of it.

PyCharm allows you to change the environment variables from the Run\Debug Configuration window. Run > Edit Configurations > Environment Variables ->

Images

omer727
  • 7,117
  • 6
  • 26
  • 39
  • 11
    thanks Omer727, I'm aware of adding vars into the test manually, and have been using this option, but was specifically wondering about why os.environ was not updating, looks like what you say about the child process not updated after it is launched is correct, seems there is no way to update without shutting down the app – RenaissanceProgrammer Dec 08 '15 at 20:21
  • 4
    perhaps I overreach... is there a way to script this? – jonathan Apr 06 '18 at 12:08
  • 5
    How to modify **parent environment variables**? Any ideas? – Bowen Xu May 16 '18 at 14:45
  • 4
    Just pay attention not to have extra spaces when defining variables directly on the field, example: `VAR1=VAL1;VAR2=VAL2` – brada Jul 15 '19 at 14:04
  • actually there is a bug that anytime I change the environments from here, they come back when I open the window again. It's really annoying – Martin Kosicky Nov 26 '19 at 14:19
15

In my case pycharm does not take env variables from bashrc even after restarting

Marat Zakirov
  • 905
  • 1
  • 8
  • 13
7

Pycharm maintains it's own version of environment variables and those aren't sourced from the shell.

It seems that if pycharm is executed from a virtualenv or the shell containing said variables, it will load with them, however it is not dynamic.

the answer below has a settings.py script for the virtualenv to update and maintain settings. Whether this completely solves your question or not i'm not sure.

Pycharm: set environment variable for run manage.py Task

Community
  • 1
  • 1
Ryan Cook
  • 169
  • 5
7

I recently discovered a workaround in windows. Close Pycharm, copy the command to run Pycharm directly from the shortcut, and rerun it in a new terminal window: cmd, cmder, etc.

C:\
λ "C:\Program Files\JetBrains\PyCharm 2017.2.1\bin\pycharm64.exe"
nmz_razor
  • 81
  • 1
  • 2
7

I know this is very late, but I encountered this issue as well and found the accepted answer tedious as I had a lot of saved configurations already.

The solution that a co-worker told me is to add the environment variables to ~/.profile instead. I then had to restart my linux machine and pycharm picked up the new values. (for OSX, I only needed to source ~/.profile and restart pycharm completely)

One thing to be aware is that another coworker said that pycharm would look at ~/.bash_profile so if you have that file, then you need the environment variables added there

Marc
  • 71
  • 1
  • 1
  • 1
    If you have a NEW question, please ask it by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. If you have sufficient reputation, [you may upvote](//stackoverflow.com/privileges/vote-up) the question. Alternatively, "star" it as a favorite and you will be notified of any new answers. – Dharmang Oct 29 '18 at 07:44
  • Didn't work for me – VErYSEYmPhY Oct 10 '22 at 07:37
1

In case you are using the "sudo python" technique, be aware that it does not by default convey the environment variables.

To correctly pass on the environment variables defined in the PyCharm launch configuration, use the -E switch:

sudo -E /path/to/python/executable "$@"
Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
0

This is simply how environment variables work. If you change them you have to re-source your .bashrc (or whatever file the environment variables are located in).

Chuck Claunch
  • 1,624
  • 1
  • 17
  • 29
  • yes that is understandable, however, resourcing the file in terminal does not update the variables in the OS object, this includes running piece of code after the source is done. only shutting down the app completely and relaunching it adds the new variables to the OS object, technically speaking, every time the code is ran, the OS object should be fresh – RenaissanceProgrammer Dec 08 '15 at 20:02
  • Again this is how environment variables work. When you load your application it's essentially loaded with a snapshot of your environment. – Chuck Claunch Dec 08 '15 at 22:48
  • ok but IMO "that's just how it works" isn't much of an answer, Omer provided some background information on WHY it works this way which is why it got my acceptance – RenaissanceProgrammer Dec 09 '15 at 18:04
  • 1
    Not really worried about acceptance, but I said the same thing he did (and before him). All he added was how to change the variables in pycharm, which is not really relevant to your question. I directly answered your question, which was giving you the reason the app was not updating when you changed the variables. Food for thought. Glad you got the answer you needed. – Chuck Claunch Dec 10 '15 at 15:57
  • How to re-source in Python after the app starts? The`os` module pulls environment variables when the `import os` command is first called. How do you re-source after the application has started? – emmdee Oct 31 '17 at 18:08
  • You don't. For the same reasons explained in the accepted answer. – Chuck Claunch Nov 01 '17 at 18:24
0

from dotenv import load_dotenv

load_dotenv(override=True)

Python-dotenv can interpolate variables using POSIX variable expansion.

With load_dotenv(override=True) or dotenv_values(), the value of a variable is the first of the values defined in the following list:

  • Value of that variable in the .env file.
  • Value of that variable in the environment.
  • Default value, if provided.
  • Empty string.

With load_dotenv(override=False), the value of a variable is the first of the values defined in the following list:

  • Value of that variable in the environment
  • Value of that variable in the .env file.
  • Default value, if provided.
  • Empty string.
nishant
  • 1
  • 1
0

Just click the EnvFile tab in the run configuration, click Enable EnvFile and click the + icon to add an env file enter image description here

tayfun Kılıç
  • 2,042
  • 1
  • 14
  • 11