【问题标题】:Pycharm set the correct environment variable PATHPycharm设置正确的环境变量PATH
【发布时间】:2014-02-05 15:21:02
【问题描述】:

我正在使用 pycharm 执行以下操作:

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

但是当我在 shell 中执行 echo $PATH 时,会返回:

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

我尝试在 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

但这不起作用

有什么想法吗?

【问题讨论】:

  • 试试os.environ['PATH']os.system("echo $PATH") ...它们应该是一样的......
  • @JoranBeasley 我想设置路径
  • file >settings 然后“project interpreter >python interpreters”选择您的解释器(可能只有一个),在屏幕的下半部分有两个选项卡...单击路径,然后单击加号添加到它...之前的评论更多是为了证明os.environ['PATH'] 应该绝对匹配echo $PATH,而不是提出解决方案
  • 请参考stackoverflow.com/a/21488010中的最佳答案

标签: python interpreter pycharm


【解决方案1】:

@fj123x,我从你的帖子中猜到你是

  1. 在 Mac 上
  2. 使用 bash 以外的 shell(可能是 zsh)

如果为 true,则问题在于 JetBrains jediterm 终端仿真器未按正确顺序执行所有 shell 启动文件。

如果您使用的是 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

如果你对所有血淋淋的细节感兴趣,或者你想看看我是如何解决这个问题的,以便你可以为另一个 shell 开发解决方案,请参阅这个答案:https://stackoverflow.com/a/51006003/1089228

【讨论】:

    【解决方案2】:

    在 Ubuntu 上,使用 zsh,我偶然发现了同样的问题。

    为了在 PyCharm 和我的 shell 中拥有相同的环境变量,我使用的 hack 是从我的终端启动 PyCharm,而不是使用图标。看起来 PyCharm shell 继承自启动它的主 shell。

    我希望它可以解决其他人的问题,因为我无法在 Linux 上复制 @Steve Tarver 的解决方案(.../terminal/.zshrc 在 /snap/ 上是只读的,即使使用 sudo 也是如此)。

    【讨论】:

    • 我在 Windows 中遇到了同样的问题。在安装了一些东西并更新了我的 PATH 之后,PyCharm 终端没有得到更新,即使在重新启动 PyCharm 之后也没有。但是,根据您的建议从终端执行 PyCharm 后,它会正确选择路径中的环境变量。之后,像往常一样从我的桌面快捷方式启动 PyCharm 工作正常并选择了所有环境变量。所以你的技巧对我有用。
    【解决方案3】:

    我在 bash 中使用命令行,我的环境(包括 $PATH)设置在 .bash_profile 中。 PyCharm 中的默认终端是tcsh。 我把它改成了 bash 文件...默认设置...工具...终端...外壳路径 然后重新凝视。嵌入式终端按预期工作。

    【讨论】:

    • PyCharm 将我的 shell 路径默认设置为 /bin/bash,但我仍然有 $PATH 问题/不匹配
    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2012-10-01
    • 2014-02-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多