2013-10-31 67 views
11

我遇到了一个问题,使我的Pycharm环境与我在命令行上具有的环境相匹配。我最近删除了Python并通过家庭酿造重新安装。我的路径中的python指向/usr/local/bin/python我将PATH=/usr/local/bin:$PATH添加到我的.bash_profile文件的开头,我可以在命令行中的interperter中正好执行以下代码。但是,当我将/usr/local/bin/python添加到项目python解释器并运行下面的代码时,我得到属性错误。任何人都可以告诉我如何让Pycharm使用与我的命令行相同的环境?Pycharm环境不同于命令行

import sqlite3 
db = "mydb.db" 
conn = sqlite3.connect(db) 
conn.enable_load_extension(True) 

AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

+2

请参阅http://stackoverflow.com/q/135688/104891。 GUI应用程序不会读取'.bash_profile'。 – CrazyCoder

+0

呃......好像每个版本的OS X都以不同的方式处理这个问题...... – CLJ

回答

15

.bash_profile是由唯一的bash(命令行解释器)读取。 但是,如果你想保留PyCharm的bash环境,那么有一个真正的Linux方法 。

从命令行(从bash)运行PyCharm。 因此环境变量将从bash继承到pycharm。 阅读$man environ有关linux环境继承过程的信息。 所以你只需要从命令行启动${PATH_TO_PYCHARM}/bin/pycharm.sh。 或创建启动PyCharm启动bash的启动程序。

那就是它!希望这对你有用。

2

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

之后,你可以使用快速午餐或其他任何方式运行pycharm,并且你的变量将会在那里。

1

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

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

This link给出了进一步的背景信息。

3

如果您正在使用PyCharm版本2016.3,并已经注意到,您的PyCharm终端不再提供相同的默认环境为您Mac系统终端环境,它是一个bug应固定在2016年3月1日 - 每当它释放。与此同时,下面是一个解决办法应该“默认”所有PyCharm项目的后面更详细的MacOS终端像PyCharm端:

使用以下内容创建〜/ .bashrc文件: source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile

应该设置您的PyCharm终端(交互式bash命令)并使其类似的MacOS终端(登录bash命令)。一旦JetBrains发布补丁并发布2016.3.1,我建议删除这个~/.bashrc文件。希望这会使我们恢复正常。

+1

不幸的是,这个bug在2017.1.2仍然存在 –

1

编辑:澄清此解决办法是PyCharm 2016.3.0

这真的建议你只在每个终端会话开始运行以下命令

source ~/.bash_profile 

,直到2016年3月1日发行了。

但是,这种错误有一个解决方法。终端脚本似乎有两个函数名称反转,所以它们必须重命名。

你必须编辑应用程序的终端插件脚本来做到这一点,这是不推荐。

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,你应该很好去。

0

对我来说,有什么工作不是从应用程序运行pycharm,而是从使用chram的终端运行。那么它继承了所有的ENV VAR和路径