13

I'm executing with pycharm the following:

print(os.environ["PATH"]) # returns '/usr/bin:/bin:/usr/sbin:/sbin'

But when I execute echo $PATH in a shell this is returned:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin

I tried to edit it in Preferences > Console > Python Console > Environment Variables, setting

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin

but this is not working

any idea?

J0HN
  • 26,063
  • 5
  • 54
  • 85
fj123x
  • 6,904
  • 12
  • 46
  • 58
  • try `os.environ['PATH']` and `os.system("echo $PATH")` ... they should be the same.... – Joran Beasley Feb 05 '14 at 15:27
  • @JoranBeasley i want to set the PATH – fj123x Feb 05 '14 at 15:53
  • file >settings then "project interpreter >python interpreters" select your interpreter (probably only one), in the bottom half og the screen there are two tabs ... click paths and then click the plus to add to it ... the earlier comment was more to demonstrate that `os.environ['PATH']` should absolutely match the `echo $PATH`, than to suggest a solution – Joran Beasley Feb 05 '14 at 16:50
  • 1
    Please, refer to best answer in http://stackoverflow.com/a/21488010 – Maberi Jul 30 '14 at 23:08

3 Answers3

3

@fj123x, I'm going to guess from your post that you are

  1. on a mac
  2. using a shell other than bash (perhaps zsh)

If true, the problem is that the JetBrains jediterm terminal emulator is not executing all shell startup files in the correct order.

If you are using zsh, you can fix that root problem by editing the terminal plugin's .zshrc. PyCharm is in your Applications folder, open /Applications/PyCharm.app/Contents/plugins/terminal/.zshrc and replace the contents with:

#!/bin/zsh

# starver mod
# Jetbrains uses jediterm as a java terminal emulator for all terminal uses.
# There are some apparent limits on use:
# - must use old-style shebang - not the #!/usr/bin/env zsh
# - must implement the startup file loading here
#
# Note: original contents are in lib/terminal.jar

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bindkey '^[^[[C' forward-word
bindkey '^[^[[D' backward-word

ZDOTDIR=$_OLD_ZDOTDIR

if [ -n "$JEDITERM_USER_RCFILE" ]
then
  source "$JEDITERM_USER_RCFILE"
  unset JEDITERM_USER_RCFILE
fi

if [ -n "$ZDOTDIR" ]
then
  DOTDIR=$ZDOTDIR
else
  DOTDIR=$HOME
fi

if [ -f "/etc/zshenv" ]; then
     source "/etc/zshenv"
fi

if [ -f "$DOTDIR/.zshenv" ]; then
     source "$DOTDIR/.zshenv"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "/etc/zprofile" ]; then
       source "/etc/zprofile"
  fi
  if [ -f "$DOTDIR/.zprofile" ]; then
       source "$DOTDIR/.zprofile"
  fi
fi

if [ -f "/etc/zshrc" ]; then
     source "/etc/zshrc"
fi

if [ -f "$DOTDIR/.zshrc" ]; then
     source "$DOTDIR/.zshrc"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "/etc/zlogin" ]; then
       source "/etc/zlogin"
  fi
  if [ -f "$DOTDIR/.zlogin" ]; then
       source "$DOTDIR/.zlogin"
  fi
fi

if [ -n "$JEDITERM_SOURCE" ]
then
  source $(echo $JEDITERM_SOURCE)
  unset JEDITERM_SOURCE
fi

If you are interested in all the gory details, or you want to see how I solved this problem so you can develop a solution for another shell, see this answer: https://stackoverflow.com/a/51006003/1089228

JIST
  • 1,139
  • 2
  • 8
  • 30
Steve Tarver
  • 3,030
  • 2
  • 25
  • 33
  • 1
    I had this issue where PyCharm's "Activate virtualenv" setting was not activating my venv after installing `oh-my-zsh` (previously it was working with stock `zsh`). After adding the `if [ -n "$JEDITERM_SOURCE" ]` block close to the bottom of my `~/.zshrc` file and opening a new terminal tab in PyCharm the venv was auto-activated as I wanted. Thanks a lot! – OscarVanL Jul 10 '23 at 14:32
2

On Ubuntu, using zsh, I stumbled upon the same problem.

The hack I use in order to have the same environment variables in PyCharm and my shell is to launch PyCharm from my terminal instead of using the icon. It looks like this way that the PyCharm shell inherits from the main shell it's been launched from.

I hope it can solve other people's problem as I wasn't able to replicate @Steve Tarver's solution on Linux (.../terminal/.zshrc was read only on /snap/, even when using sudo).

Pierre Gourseaud
  • 2,347
  • 13
  • 24
  • 1
    I had the same problem in Windows. After installing some things and updating my PATH, PyCharm terminal was not getting updated, not even after restarting PyCharm. However, after executing PyCharm from the terminal as per your recommendation it picked the environment variables form the Path correctly. After that, starting PyCharm from my Desktop shortcut as usual worked fine and picked all the environment variables. So your hack worked for me. – Ignacio Feb 11 '19 at 17:50
1

I work on the command line in bash and my environment, including $PATH, is set in .bash_profile. The default terminal in PyCharm is tcsh. I changed it to bash by going File ... Default Settings ... Tools ... Terminal ... Shell Path and then restaring. The embedded terminal worked as expected.

BlackM
  • 3,927
  • 8
  • 39
  • 69
pmcd
  • 27
  • 2
  • 9
    PyCharm had my shell path set to `/bin/bash` by default, and I still have `$PATH` issues/mismatches – KFunk Apr 13 '16 at 19:50