Python Pycharm 设置正确的环境变量PATH
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21581197/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pycharm set the correct environment variable PATH
提问by fj123x
I'm executing with pycharm the following:
我正在使用 pycharm 执行以下操作:
print(os.environ["PATH"]) # returns '/usr/bin:/bin:/usr/sbin:/sbin'
But when I execute echo $PATH in a shell this is returned:
但是当我在 shell 中执行 echo $PATH 时,会返回:
/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
我尝试在 Preferences > Console > Python Console > Environment Variables 中编辑它,设置
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
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?
任何的想法?
回答by pmcd
I work on the command line in bash and my environment, including $PATH, is set in .bash_profile. The default terminal in PyCharmis tcsh. 
I changed it to bash by going
File ... Default Settings ... Tools ... Terminal ... Shell Pathand then restaring. The embedded terminal worked as expected.
我在 bash 中的命令行上工作,我的环境(包括$PATH)设置在.bash_profile. PyCharm 中的默认终端是tcsh. 我通过转到文件...默认设置...工具...终端...外壳路径然后重新启动将其更改为bash
 。嵌入式终端按预期工作。
回答by Steve Tarver
@fj123x, I'm going to guess from your post that you are
@fj123x,我会从你的帖子中猜测你是
- on a mac
 - using a shell other than bash (perhaps zsh)
 
- 在 Mac 上
 - 使用 bash 以外的 shell(可能是 zsh)
 
If true, the problem is that the JetBrains jediterm terminal emulator is not executing all shell startup files in the correct order.
如果为 true,则问题在于 JetBrains jediterm 终端仿真器未按正确顺序执行所有 shell 启动文件。
If you are using zsh, you can fix that root problem by editing the terminal plugin's .zshrc. Assuming PyCharn is in your Applications folder, open /Applications/PyCharm.app/Contents/plugins/terminal/.zshrcand replace the contents with:
如果您使用的是 zsh,则可以通过编辑终端插件的.zshrc. 假设 PyCharn 在您的 Applications 文件夹中,打开/Applications/PyCharm.app/Contents/plugins/terminal/.zshrc并将内容替换为:
#!/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
如果您对所有血腥细节感兴趣,或者想看看我是如何解决这个问题的,以便为另一个 shell 开发解决方案,请参阅此答案:https: //stackoverflow.com/a/51006003/1089228
回答by Pierre Gourseaud
On Ubuntu, using zsh, I stumbled upon the same problem.
在 Ubuntu 上,使用 zsh,我偶然发现了同样的问题。
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.
我为了在 PyCharm 和我的 shell 中拥有相同的环境变量而使用的 hack 是从我的终端启动 PyCharm 而不是使用图标。看起来 PyCharm shell 从它启动的主 shell 继承了这种方式。
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).
我希望它可以解决其他人的问题,因为我无法在 Linux 上复制 @Steve Tarver 的解决方案(.../terminal/.zshrc 只能在 /snap/ 上读取,即使使用 sudo 也是如此)。

