Pycharm环境与命令行环境不同

25
我在使用Pycharm时遇到了问题,无法与命令行上的环境匹配。我最近通过Homebrew卸载并重新安装了Python。我的路径中指向/usr/local/bin/python。我在 .bash_profile文件的开头添加了PATH=/usr/local/bin:$PATH,并且我可以在命令行解释器中很好地执行以下代码。然而,当我将/usr/local/bin/python添加到项目Python解释器并运行以下代码时,我收到了属性错误。请问有谁能够解决这个问题并让Pycharm使用与命令行相同的环境呢?
import sqlite3
db = "mydb.db"
conn = sqlite3.connect(db)
conn.enable_load_extension(True)

AttributeError: 'sqlite3.Connection' 对象没有属性 'enable_load_extension'


2
参考 https://dev59.com/3nVC5IYBdhLWcg3w9GHM。GUI 应用程序不会读取您的 .bash_profile 文件。 - CrazyCoder
呃...似乎每个版本的OS X都以不同的方式处理这个问题... - CLJ
9个回答

29

.bash_profile文件仅由bash(您的命令行解释器)读取。 然而,如果您想为PyCharm保留bash环境,则有一种真正的Linux方法。

从命令行(从bash)运行PyCharm。 因此,环境变量将从bash继承到pycharm。请阅读$manenviron以获取有关Linux环境继承过程的信息。 所以您所需要的就是从命令行启动${PATH_TO_PYCHARM}/bin/pycharm.sh。 或者创建启动程序,调用bash来启动PyCharm。

就是这样!希望对您有所帮助。


1
当我在路径{PATH_TO_PYCHARM}/bin下运行./pycharm.sh时,它可以正常工作。你知道如何创建一个桌面启动器,让Pycharm也能很好地工作吗? - Jason
从@Sachin G.的答案中可以得知,将路径添加到~/.bashrc文件中可以使生活变得更加轻松(https://dev59.com/yF8d5IYBdhLWcg3wNAGy): 问题已经得到解答,更新答案以将PyCharm bin目录添加到$PATH变量中,以便可以在终端的任何位置(path)打开pycharm编辑器。编辑bashrc文件, `nano .bashrc` 在bashrc文件末尾添加以下行 `export PATH="/bin:$PATH"` 现在您可以在终端的任何位置打开pycharm `pycharm.sh` - Jason
我需要做两个更改:在文件顶部,将#!/bin/sh更改为#!/bin/bash -i(交互式!)。需要Bash来使用source命令,需要交互式Bash以自动源化~/.bashrc。可选地,您可以在底部执行其他脚本。 - undefined

4
如果您正在使用PyCharm 2016.3版本,并且发现您的 PyCharm终端不再提供与 MacOS终端环境相同的默认环境,则这是一个错误,应在发布2016.3.1时进行修复。同时,以下是一种解决方法,可以将所有PyCharm项目“默认”回到更类似于MacOS-Terminal的PyCharm-Terminal: 创建一个包含以下内容的~/.bashrc文件: source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile 这样应该可以设置您的PyCharm终端(交互式bash会话),并使其类似于MacOS终端(登录bash会话)。一旦JetBrains修补并发布了2016.3.1,建议删除此~/.bashrc文件。希望这能让我们都恢复正常。

1
很遗憾,截至2017.1.2版本,这个bug仍然存在。 - Jason Wolosonovich

3

另一种方法是通过在PY_CHARM_INSTALL_DIR/bin/pycharm.sh中添加行. /path/to/script设置环境变量(例如.bash_profile)来获取您的脚本。

之后,您可以使用快速启动或其他方式运行pycharm,并且您的变量将存在其中。


2

编辑:澄清这个解决方法适用于 PyCharm 2016.3.0

建议您只运行以下命令

source ~/.bash_profile

直到2016.3.1版本发布前,在每个终端会话开始时都需要执行此操作。

然而,这个 bug 有一个解决方法。终端脚本似乎将两个函数名反转了,因此它们必须被重命名。

你需要编辑应用程序的终端插件脚本来完成此操作,但不建议这样做。

在 MacOSX 上,如果 PyCharm 是全局安装的(否则不确定在哪里),可以在此处找到:

cd /Applications/PyCharm.app/Contents/plugins/terminal

用您选择的文本处理器编辑“jediterm-bash.in”文件。它应该看起来像这样:

#!/bin/bash

function load_login_configs {
  #       When bash is invoked as an interactive login shell, or as a  non-interac-
  #       tive  shell with the --login option, it first reads and executes commands
  #       from the file /etc/profile, if that  file  exists.   After  reading  that
  #       file,  it  looks  for  ~/.bash_profile, ~/.bash_login, and ~/.profile, in
  #       that order, and reads and executes  commands  from  the  first  one  that
  #       exists  and  is  readable.

  if [ -f /etc/profile ]; then
     source /etc/profile
  fi

  if [ -f ~/.bash_profile ]; then
     source ~/.bash_profile
  else
     if [ -f ~/.bash_login ]; then
        source ~/.bash_login
     else
        if [ -f ~/.profile ]; then
           source ~/.profile
        fi
     fi
  fi
}

function load_interactive_configs {
  if [ -f ~/.bashrc ]; then
       source ~/.bashrc
  fi
}

if [ `shopt -q login_shell` ]; then
  load_login_configs
fi

load_interactive_configs

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bind '"\e\e[C":forward-word'
bind '"\e\e[D": backward-word'
bind '"\e\O[C":forward-word'
bind '"\e\O[D": backward-word'

function generate_command_executed_sequence() {
   printf '\e\7'
}

export -f generate_command_executed_sequence


#generate escape sequence after command is executed to notify jediterm emulator
trap "generate_command_executed_sequence" DEBUG

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

if [ -n "$JEDITERM_SOURCE" ]
then
  source $JEDITERM_SOURCE
fi

将以下函数重命名:

load_login_configs => load_interactive_configs

load_interactive_configs => load_login_configs

最终脚本应为:

#!/bin/bash

function load_interactive_configs {
  #       When bash is invoked as an interactive login shell, or as a  non-interac-
  #       tive  shell with the --login option, it first reads and executes commands
  #       from the file /etc/profile, if that  file  exists.   After  reading  that
  #       file,  it  looks  for  ~/.bash_profile, ~/.bash_login, and ~/.profile, in
  #       that order, and reads and executes  commands  from  the  first  one  that
  #       exists  and  is  readable.

  if [ -f /etc/profile ]; then
     source /etc/profile
  fi

  if [ -f ~/.bash_profile ]; then
     source ~/.bash_profile
  else
     if [ -f ~/.bash_login ]; then
        source ~/.bash_login
     else
        if [ -f ~/.profile ]; then
           source ~/.profile
        fi
     fi
  fi
}

function load_login_configs {
  if [ -f ~/.bashrc ]; then
       source ~/.bashrc
  fi
}

if [ `shopt -q login_shell` ]; then
  load_login_configs
fi

load_interactive_configs

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bind '"\e\e[C":forward-word'
bind '"\e\e[D": backward-word'
bind '"\e\O[C":forward-word'
bind '"\e\O[D": backward-word'

function generate_command_executed_sequence() {
   printf '\e\7'
}

export -f generate_command_executed_sequence


#generate escape sequence after command is executed to notify jediterm emulator
trap "generate_command_executed_sequence" DEBUG

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

if [ -n "$JEDITERM_SOURCE" ]
then
  source $JEDITERM_SOURCE
fi

保存并重新启动PyCharm,然后您就可以开始使用了。

1

1

很不幸,在Mac OS X中,图形应用程序不会继承您的.bash_profile配置。我将发布更新的解决方法,适用于OSX 10.11,以设置GUI级别的环境变量:

有一个有用的存储库称为osx-env-sync,它读取~/.bash_profile并为GUI应用程序设置导出的环境变量。在复制2个文件并运行github页面上描述的另外2个命令后,可以使用全局变量快速启动Pycharm,这些变量如在bash_profile中定义。

此链接提供更多背景信息。


0

我是个傻瓜,完全忘记了我正在虚拟环境中工作!在终端上运行source /.profile命令需要在已激活的虚拟环境中运行!我认为有些人可能会像我一样经历艰难的一天(或类似的脑功能障碍),并发现这很有帮助。


-1
对我来说有效的方法是不要从应用程序中运行PyCharm,而是使用终端运行chram。然后它会继承所有的环境变量和路径。

-1

我在OSX上使用Pycharm。在Pycharm > Preferences > Terminal中,shell路径为/bin/zsh。 但是这个shell环境与我的其他终端仿真器(如iTerm)不同步。 例如,我的conda环境没有被初始化(甚至连基本环境也没有)。

解决方案:将shell路径从/bin/zsh更改为/usr/bin/env zsh可以解决问题。 对于bash,将shell路径添加为/usr/bin/env bash应该可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接