50

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

4
  • Curious... how often do you update environment vars? Also, you might have better luck at superuser.com. Commented Dec 1, 2015 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 Commented Dec 1, 2015 at 20:12
  • Would the problem be solved by reloading the interpreter as described here : jetbrains.com/pycharm/help/… ? Commented Dec 5, 2015 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 Commented Dec 8, 2015 at 20:19

9 Answers 9

28
+50

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

Sign up to request clarification or add additional context in comments.

5 Comments

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
perhaps I overreach... is there a way to script this?
How to modify parent environment variables? Any ideas?
Just pay attention not to have extra spaces when defining variables directly on the field, example: VAR1=VAL1;VAR2=VAL2
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
19

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

3 Comments

Isn't bashrc run only when you start up a terminal?
For the best of my knowledge pycharm inherit environment variables if started from terminal
I have this problem. Does anyone know a fix?
8

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

Comments

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"

Comments

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

2 Comments

If you have a NEW question, please ask it by clicking the Ask Question button. If you have sufficient reputation, you may upvote the question. Alternatively, "star" it as a favorite and you will be notified of any new answers.
Didn't work for me
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 "$@"

Comments

1

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.

Comments

1

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

Comments

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

6 Comments

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
Again this is how environment variables work. When you load your application it's essentially loaded with a snapshot of your environment.
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
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.
How to re-source in Python after the app starts? Theos module pulls environment variables when the import os command is first called. How do you re-source after the application has started?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.