简体   繁体   English

Pycharm 环境不同于命令行

[英]Pycharm environment different than command line

I am having an issue getting my Pycharm environment to match up with the environment that I have on the command line.我在让 Pycharm 环境与命令行上的环境相匹配时遇到问题。 I recently removed python and reinstalled it via home brew.我最近删除了 python 并通过自制软件重新安装了它。 The python in my path is pointing to /usr/local/bin/python I added PATH=/usr/local/bin:$PATH to the beginning of my .bash_profile file and I can execute the following code just fine in the interperter on the command line.我路径中的 python 指向/usr/local/bin/python我添加了PATH=/usr/local/bin:$PATH到我的 .bash_profile 文件的开头,我可以在 interperter 上执行以下代码就好了命令行。 However, when I add /usr/local/bin/python to the project python interpreters and run the below code I get the attribute error.但是,当我将/usr/local/bin/python到项目 python 解释器并运行以下代码时,我得到了属性错误。 Can anyone shed some light on how I can get Pycharm to use the same environment as my command line?谁能解释一下我如何让 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' AttributeError: 'sqlite3.Connection' 对象没有属性 'enable_load_extension'

.bash_profile is being read by bash (your command line interpreter) only. .bash_profile仅由 bash(您的命令行解释器)读取。 However if you want to preserve bash environment for PyCharm there is one true Linux way.但是,如果您想为 PyCharm 保留 bash 环境,则有一种真正的 Linux 方式。

Run PyCharm from your command line (from bash).从命令行(从 bash)运行 PyCharm。 Thus environment variables will be inherited from bash to pycharm.因此环境变量将从 bash 继承到 pycharm。 Read $man environ for information on linux environment inheritance process.阅读$man environ 以获取有关 linux 环境继承过程的信息。 So all you need is just launch ${PATH_TO_PYCHARM}/bin/pycharm.sh from command line.所以你所需要的只是${PATH_TO_PYCHARM}/bin/pycharm.sh启动${PATH_TO_PYCHARM}/bin/pycharm.sh Or create launcher which invokes bash for PyCharm launching.或者创建启动器来调用 bash 来启动 PyCharm。

Thats it !就是这样! Hope that works for you.希望这对你有用。

If you are using PyCharm version 2016.3 and have noticed that your PyCharm Terminal is no longer providing the same default environment as your MacOs Terminal environment, it is a bug that should be fixed in 2016.3.1 - whenever it releases.如果您使用的PyCharm版本2016.3,并已经注意到,您的PyCharm终端不再提供相同的默认环境为您的Mac系统终端环境,这是一个错误应该被固定在2016年3月1日-只要它释放。 In the mean time, the following is a workaround that should 'default' all of your PyCharm projects back a more more MacOS-Terminal like PyCharm-Terminal:同时,以下是一种解决方法,它应该“默认”您的所有 PyCharm 项目支持更像 PyCharm-T​​erminal 的 MacOS 终端:

Create a ~/.bashrc file with the following contents: source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile创建一个 ~/.bashrc 文件,内容如下: source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile

This should setup your PyCharm Terminal ( interactive bash session ) and make it similar to the MacOS Terminal ( login bash session ).应该设置您的 PyCharm 终端(交互式 bash 会话)并使其类似于 MacOS 终端(登录 bash 会话)。 Once JetBrains patches and releases 2016.3.1, I recommend deleting this ~/.bashrc file.一旦 JetBrains 修补并发布 2016.3.1,我建议删除这个~/.bashrc文件。 Hopefully this will get us all back to normal.希望这能让我们都恢复正常。

Another approach is to source your script setting environmental variables (for example .bash_profile ) by adding a line . /path/to/script另一种方法是通过添加一行来获取脚本设置环境变量(例如.bash_profile. /path/to/script . /path/to/script into PY_CHARM_INSTALL_DIR/bin/pycharm.sh . . /path/to/scriptPY_CHARM_INSTALL_DIR/bin/pycharm.sh

After that you can run pycharm using quick-lunch or whatever and you variables will be there.之后,您可以使用 quick-lunch 或其他方式运行 pycharm,您的变量将在那里。

Edit: clarifying that this workaround is for PyCharm 2016.3.0编辑:澄清此解决方法适用于PyCharm 2016.3.0

It's really recommended that you just run the following command真的建议您只运行以下命令

source ~/.bash_profile

at the start of each terminal session until 2016.3.1 is released.在每个终端会话开始时,直到 2016.3.1 发布。

However, there is a workaround for this bug.但是,此错误有一个解决方法。 The terminal script appears to have 2 function names reversed, so they must be renamed.终端脚本似乎有 2 个颠倒的函数名称,因此必须重命名它们。

You have to edit the app's terminal plugin script to do this, which is NOT recommended.您必须编辑应用程序的终端插件脚本才能执行此操作,这是不推荐的。

On MacOSX, this is located here if PyCharm is installed globally (not sure where otherwise):在 MacOSX 上,如果 PyCharm 是全局安装的,则它位于此处(其他情况不确定):

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

Edit the 'jediterm-bash.in' file with the text processor of your choice.使用您选择的文本处理器编辑“jediterm-bash.in”文件。 If should look like this:如果应该是这样的:

#!/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

Rename the following functions:重命名以下函数:

load_login_configs => load_interactive_configs load_login_configs => load_interactive_configs

load_interactive_configs => load_login_configs load_interactive_configs => load_login_configs

The final script should be:最后的脚本应该是:

#!/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

Save and restart PyCharm and you should be good to go.保存并重新启动 PyCharm,您应该一切顺利。

Unfortunately in Mac OS X, graphic applications do not inherit your .bash_profile config.不幸的是,在 Mac OS X 中,图形应用程序不会继承您的 .bash_profile 配置。 I'm posting the updated workaround for OSX 10.11 on how to set up environment variables at GUI level:我正在发布有关如何在 GUI 级别设置环境变量的 OSX 10.11 的更新解决方法:

There is a useful repo called osx-env-sync which reads ~/.bash_profile and sets the environment variables exported in it for GUI applications.有一个名为osx-env-sync的有用存储库,它读取 ~/.bash_profile 并设置其中为 GUI 应用程序导出的环境变量。 After copying 2 files and running 2 other commands described on the github page, Pycharm can be launched in quick-start with global variables available as defined in the bash_profile.复制 2 个文件并运行 github 页面上描述的 2 个其他命令后,可以使用 bash_profile 中定义的全局变量快速启动 Pycharm。

This link gives further background information. 链接提供了进一步的背景信息。

I actually just found a solution for this that works in PyCharm 2017.1.2我实际上刚刚找到了一个适用于 PyCharm 2017.1.2 的解决方案

uncheck Tools > Terminal > "shell integration"取消选中“工具”>“终端”>“外壳集成”

source: answer from @Federicojama near the bottom of the page https://intellij-support.jetbrains.com/hc/en-us/community/posts/208567485-Pycharm-terminal-is-missing-part-of-PATH来源:来自页面底部附近的@Federicojama 的回答https://intellij-support.jetbrains.com/hc/en-us/community/posts/208567485-Pycharm-terminal-is-missing-part-of-PATH

for me what worked was not running pycharm from the applications but from a terminal using chram .对我来说,有效的不是从应用程序运行 pycharm,而是从使用 chram 的终端运行。 then it inherited all the env vars and paths然后它继承了所有的环境变量和路径

I am using pycharm on OSX.我在 OSX 上使用 pycharm。 Pycharm > Preferences > Terminal had shell path as /bin/zsh . Pycharm > Preferences > Terminal shell 路径为/bin/zsh But this shell environment was not in sync with my other terminal emulators like iTerm.但是这个 shell 环境与我的其他终端模拟器(如 iTerm)不同步。 For example, my conda environment was not getting initialized (not even the base environment).例如,我的 conda 环境没有被初始化(甚至没有被初始化)。

Solution: Changing the shell path from /bin/zsh to /usr/bin/env zsh fixed things for me.解决方案:将 shell 路径从/bin/zsh更改为/usr/bin/env zsh为我解决了问题。 For bash , adding shell path as /usr/bin/env bash should work.对于bash ,将 shell 路径添加为/usr/bin/env bash应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM