测试人社区

PyCharm 内建终端自动激活虚拟环境失败

打开或新建 PyCharm 的内建 Terminal 时会报错

/Applications/PyCharm CE.app/Contents/plugins/terminal/.zshrc:source:44: no such file or directory: /Users/zhengbangbo/Library/Mobile

之后,我找了一下这个路径,发现它可能就是我当前项目的路径

/Users/zhengbangbo/Library/Mobile\ Documents/com~apple~CloudDocs/Projects/Selenium_PO_EnterpriseWechat/venv/bin/activate

这是一个iCloud里面的文件夹,其中 Mobile\ Documents 包含一个空格。我估计是配置文件中对于空格的转义没有做好,导致出现了这个问题。

以下是我的配置文件

#!/bin/bash

# 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 "$DOTDIR/.zshenv" ]; then
     source "$DOTDIR/.zshenv"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "$DOTDIR/.zprofile" ]; then
       source "$DOTDIR/.zprofile"
  fi
fi

if [ -f "$DOTDIR/.zshrc" ]; then
     source "$DOTDIR/.zshrc"
fi

if [ -n $LOGIN_SHELL ]; then
  if [ -f "$DOTDIR/.zlogin" ]; then
       source "$DOTDIR/.zlogin"
  fi
fi

if [ -n "$JEDITERM_SOURCE" ]
then
  source $(echo $JEDITERM_SOURCE) $JEDITERM_SOURCE_ARGS  # !!!这里是第44行!!!
  unset JEDITERM_SOURCE
  unset JEDITERM_SOURCE_ARGS
fi

function override_jb_variables {
  env | while read VARIABLE
  do
    NAME=${VARIABLE%%=*}
    if [[ $NAME = '_INTELLIJ_FORCE_SET_'* ]]
    then
      NEW_NAME=${NAME:20}
      if [ -n "$NEW_NAME" ]
      then
        VALUE=${VARIABLE#*=}
        export "$NEW_NAME"="$VALUE"
      fi
    fi
  done
}

override_jb_variables

这个配置文件我没有手动配置过,也看不太懂。

问题找到了,但是不知道怎么解决。请大家帮忙!

只觉得自己太笨,明明很简单调试一下就好了,硬是当时想不通。

解放思路

先是把两个变量的结果打印出来

echo "\$JEDITERM_SOURCE", $JEDITERM_SOURCE
echo "\$JEDITERM_SOURCE_ARGS", $JEDITERM_SOURCE_ARGS

结果是

$JEDITERM_SOURCE, /Users/zhengbangbo/Library/Mobile Documents/com~apple~CloudDocs/Projects/Selenium_PO_EnterpriseWechat/venv/bin/activate
$JEDITERM_SOURCE_ARGS,

那么很简单,加个双引号就OK了

# Before
source $(echo $JEDITERM_SOURCE) $JEDITERM_SOURCE_ARGS
# After
source "$(echo $JEDITERM_SOURCE)" $JEDITERM_SOURCE_ARGS