0

I want to call an app from its bash command using Python. I added the path to the app to the PATH environment variable, so that this work in the terminal. However, when doing this in Pycharm, it does not work. I discovered, that is because apparently the environment variables are not all loaded in, when I call the app in Pycharm.

This is what I do in the terminal:

(save) philipp@philipp-Blade-15-Advanced-Model-Early-2020:~$ python Python 3.8.2 (default, Mar 25 2020, 17:03:02) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ["PATH"] '/home/philipp/anaconda3/envs/save/bin:/home/philipp/anaconda3/condabin:/home/philipp/sumo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin' 

When I run the same command in Pycharm, I get this output:

'/home/philipp/anaconda3/envs/save/bin:/home/philipp/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin' 

The output from Pycharm is missing /home/philipp/sumo/bin which is where I installed my app and where I need to call it from. I looked into this a lot, but could not figure out what the issue would be... I am using the same Anaconda environment in both cases. Can you point out the issue?

7
  • 1
    It appears that the first two directories are added by your virtual environment, while /home/philipp/sumo/bin is added by your shell's configuration files. PyCharm isn't being started by your login shell, and so doesn't inherit a path containing sumo/bin. Commented Feb 8, 2021 at 18:32
  • @chepner Yes, I am adding /home/philipp/sumo/bin to PATH in the .bashrc file. Can you tell me, how I can use the same shell which the terminal uses in PyCharm as well? Also, in Windows this was working by default, I only came across the problem in Linux. Commented Feb 8, 2021 at 19:03
  • This answer (stackoverflow.com/questions/27553576/…) says, that the .bashrc is loaded into os.environ on login. Evidently that cannot be true. Commented Feb 8, 2021 at 19:09
  • 1
    More or less, .bashrc is sourced when your shell starts. But PyCharm doesn't have your shell as the parent; it basically gets executed for you by another process, so it doesn't inherit from your login shell. Commented Feb 8, 2021 at 19:15
  • @chepner Ok, thanks. Then how do I insert the environment variables from bashrc when PyCharm starts a new shell? Also is there a Platform Independent method? Can you write an answer, so that I can upvote it? Commented Feb 8, 2021 at 19:25

1 Answer 1

1

Your thing works for me. What I did was:

  1. I edited my .bashrc with export PATH=/THIS/IS/TEST/PATH:$PATH in the last line.
  2. Exited the terminal and created a new one to refresh my instance.
  3. Checked if path is properly loaded with /THIS/IS/TEST/PATH using echo $PATH.
  4. Opened up pycharm from current terminal using charm command.
  5. Copy+Pasted your code
import os print(os.environ["PATH"]) 
  1. Checked the printed output and I found the /THIS/IS/TEST/PATH.

Im running from a single terminal and using anaconda as well

Are you running pycharm from the terminal ?


(Alternative solution)

If it still doesn't work for you, why not just create a txt file that holds the PATH variable and refreshes whenever ~/.bashrc was called.

for example,

  1. in the ~/.bashrc file, put this in the last line:
echo "$PATH" > ~/bash_path.txt 
  1. then just read the content as a variable in your python code
PATH = open('/absolute/path/to/bash_path.txt', 'r').read() print(PATH) 

which should have the same output as with the os.environ command.

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

2 Comments

The first solution works for me. I did not know running PyCharm from the terminal was possible and that is would initialize the PATH env variable from the .bashrc. I also found, that one can input environment variables, which PyCharm then uses on startup (see here stackoverflow.com/questions/42708389/…)
That's good to know! I think the 2nd solution can be used in tandem with the pycharm environment setup you suggested. From what I saw from the link you sent, it still needs an env file (which is what the 2nd solution creates).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.