Pycharm环境不同于命令行

s8vozzvw  于 2023-04-06  发布在  PyCharm
关注(0)|答案(9)|浏览(204)

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

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

属性错误:“sqlite3.Connection”对象没有属性“enable_load_extension”

pbpqsu0x

pbpqsu0x1#

.bash_profile只被bash(你的命令行解释器)读取。但是如果你想为PyCharm保留bash环境,有一个真正的Linux方法。
从命令行(从bash)运行PyCharm。因此环境变量将从bash继承到pycharm。阅读$man environ以了解Linux环境继承过程的信息。因此,您所需要的只是从命令行启动${PATH_TO_PYCHARM}/bin/pycharm.sh。或者创建启动器,调用bash启动PyCharm。
就是这样!希望对你有用。

gmol1639

gmol16392#

如果您使用的是PyCharm 2016.3版本,并且注意到您的PyCharm终端不再提供与您的MacOs终端环境相同的默认环境,则应在2016.3.1版本中修复bug-无论何时发布。同时,以下是一个解决方案,应该“默认”所有PyCharm项目都支持更像PyCharm-Terminal的MacOS-Terminal:

创建一个~/.bashrc文件,内容如下:source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile

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

nhaq1z21

nhaq1z213#

另一种方法是通过在PY_CHARM_INSTALL_DIR/bin/pycharm.sh中添加一行. /path/to/script来获取设置环境变量(例如.bash_profile)的脚本。
之后,你可以使用quick-lunch或其他方法运行pycharm,你的变量就会在那里。

vybvopom

vybvopom4#

编辑:澄清此解决方案适用于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,你应该可以开始了。

klsxnrf1

klsxnrf15#

不幸的是,在Mac OS X中,图形应用程序不会继承你的.bash_profile配置。我发布了OSX 10.11的更新解决方案,关于如何在GUI级别设置环境变量:
有一个有用的repo叫做osx-env-sync,它读取~/.bash_profile,并为GUI应用程序设置导出的环境变量。在复制2个文件并运行github页面上描述的其他2个命令后,Pycharm可以在快速启动中启动,并提供bash_profile中定义的全局变量。
This链接提供了进一步的背景信息。

nimxete2

nimxete26#

我实际上刚刚找到了一个在PyCharm 2017.1.2中工作的解决方案
取消勾选工具〉终端〉“shell集成”
来源:来自@Federicojama的回答,靠近页面底部https://intellij-support.jetbrains.com/hc/en-us/community/posts/208567485-Pycharm-terminal-is-missing-part-of-PATH

w1e3prcc

w1e3prcc7#

我是一个白痴,完全忘记了我在虚拟环境中工作!source /.profile命令需要在终端中激活的虚拟环境中运行!我想有些人可能有一天很辛苦(或类似的大脑功能障碍),并发现这很有帮助。

omqzjyyz

omqzjyyz8#

对我来说,工作不是从应用程序运行pycharm,而是从使用chram的终端运行。然后它继承了所有的env变量和路径

uurity8g

uurity8g9#

我在OSX上使用pycharm。Pycharm > Preferences > Terminal的shell路径为/bin/zsh。但是这个shell环境与我的其他终端模拟器(如iTerm)不同步。例如,我的conda环境没有初始化(甚至没有基本环境)。
解决方案:将shell路径从/bin/zsh更改为/usr/bin/env zsh对我来说解决了一些问题。对于bash,将shell路径添加为/usr/bin/env bash应该可以。

相关问题