当我激活virtualenv时,为什么我的提示不会改变?

时间:2016-04-06 17:45:59

标签: python bash virtualenv command-prompt virtualenvwrapper

当我使用workon激活Python virtualenv时,我的提示不会更改以识别环境。

我已确认我的activate脚本包含

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x" != x ] ; then
        PS1="$PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
    fi
    export PS1
fi

并且VIRTUAL_ENV_DISABLE_PROMPT未设置。

为什么会发生这种情况,我该怎么做才能解决它?

GNU bash,版本3.2.57(1)-release(x86_64-apple-darwin15)

我的.bash_profile中有一些花哨的步法,但我不明白为什么在设置提示后这一点有任何意义:

declare PSCOLORPATH='\[\e[0;33m\]'
declare PSCOLORPROMPT='\[\e[1;32m\]'
declare PSCOLORHOST='\[\e[0;32m\]'
declare PSCOLOROOT='\[\e[1;31m\]'
declare PSCOLOREND='\[\e[m\]'
#echo $PSCOLORPATH
if [ $SSH_FLAG -eq 1 ] ; then
    declare PSHOST='\h:'
else
    declare PSHOST=''
fi
if [ $IS_SU -eq 1 ] ; then
    PS1_INFO="$PSCOLOROOT[SU] $PSCOLORHOST$PSHOST$PSCOLORPATH\w"
else
    PS1_INFO="$PSCOLORHOST$PSHOST$PSCOLORPATH\w"
fi
PS1_PROMPT="$PSCOLORPROMPT \\$ $PSCOLOREND"
declare -x PS1="$PS1_INFO$PS1_PROMPT"
#declare -x PS1="$PSCOLORHOST$PSHOST$PSCOLORPATH\w $PSCOLORPROMPT\\$ $PSCOLOREND"
declare -x PS2="$PSCOLORPROMPT> $PSCOLOREND"
declare -x PS4="$PSCOLORPROMPT+ $PSCOLOREND"

source $(brew --repository)/etc/bash_completion.d/git-prompt.sh

GIT_PS1_HIDE_IF_PWD_IGNORED=1
PS1_NORMAL=$PS1
_gitprompt () {
    if [ "$1" = "off" ]; then
        GIT_PS1_SHOWUPSTREAM=; GIT_PS1_SHOWDIRTYSTATE=; GIT_PS1_SHOWUNTRACKEDFILES=; GIT_PS1_SHOWCOLORHINTS=;
        PS1="$PS1_NORMAL"; PROMPT_COMMAND=; 
    else
        if [ "$1" = "verbose" ]; then
            GIT_PS1_SHOWUPSTREAM="auto"; GIT_PS1_SHOWDIRTYSTATE=1; GIT_PS1_SHOWUNTRACKEDFILES=1; GIT_PS1_SHOWCOLORHINTS=;
            PROMPT_COMMAND='__git_ps1 "$PS1_INFO" "$PS1_PROMPT"';           
        elif [ "$1" = "color" ]; then
            GIT_PS1_SHOWUPSTREAM=; GIT_PS1_SHOWDIRTYSTATE=; GIT_PS1_SHOWUNTRACKEDFILES=; GIT_PS1_SHOWCOLORHINTS=1;
            PROMPT_COMMAND='__git_ps1 "$PS1_INFO$PSCOLOREND" "$PS1_PROMPT"';            
        else
            GIT_PS1_SHOWUPSTREAM=; GIT_PS1_SHOWDIRTYSTATE=; GIT_PS1_SHOWUNTRACKEDFILES=; GIT_PS1_SHOWCOLORHINTS=;
            PROMPT_COMMAND='__git_ps1 "$PS1_INFO" "$PS1_PROMPT"';
        fi
    fi
}
alias gitprompt="_gitprompt"

gitprompt verbose

FWIW,set -x; source /path/to/activate给出:

+ __git_ps1 '\[\e[0;32m\]\[\e[0;33m\]\w' '\[\e[1;32m\] \$ \[\e[m\]'
+ local exit=126
+ local pcmode=no
+ local detached=no
+ local 'ps1pc_start=\u@\h:\w '
+ local 'ps1pc_end=\$ '
+ local 'printf_format= (%s)'
+ case "$#" in
+ pcmode=yes
+ ps1pc_start='\[\e[0;32m\]\[\e[0;33m\]\w'
+ ps1pc_end='\[\e[1;32m\] \$ \[\e[m\]'
+ printf_format=' (%s)'
+ PS1='\[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'
+ local ps1_expanded=yes
+ '[' -z '' ']'
+ '[' -z '3.2.57(1)-release' ']'
+ shopt -q promptvars
+ local repo_info rev_parse_exit_code
+ git rev-parse --git-dir --is-inside-git-dir --is-bare-repository --is-inside-work-tree --short HEAD
+ repo_info=
+ rev_parse_exit_code=128
+ '[' -z '' ']'
+ return 126
~ $ set -x; source ~/Documents/Projects/Coding/Python/venvs/zoot/bin/activate
+ set -x
+ source /Users/Rax/Documents/Projects/Coding/Python/venvs/zoot/bin/activate
+ deactivate nondestructive
+ unset -f pydoc
+ '[' -z _ ']'
+ PATH=/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin
+ export PATH
+ unset _OLD_VIRTUAL_PATH
+ '[' -z '' ']'
+ '[' -n /bin/bash ']'
+ hash -r
+ '[' -z _ ']'
+ PS1='\[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'
+ export PS1
+ unset _OLD_VIRTUAL_PS1
+ unset VIRTUAL_ENV
+ '[' '!' nondestructive = nondestructive ']'
+ VIRTUAL_ENV=/Users/Rax/Documents/Projects/Coding/Python/venvs/zoot
+ export VIRTUAL_ENV
+ _OLD_VIRTUAL_PATH=/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin
+ PATH=/Users/Rax/Documents/Projects/Coding/Python/venvs/zoot/bin:/Users/Rax/.cabal/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Rax/bin:/opt/X11/bin:/Library/TeX/texbin
+ export PATH
+ '[' -z '' ']'
+ '[' -z '' ']'
+ _OLD_VIRTUAL_PS1='\[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'
+ '[' x '!=' x ']'
+ basename /Users/Rax/Documents/Projects/Coding/Python/venvs/zoot
+ PS1='(zoot) \[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'
+ export PS1
+ alias pydoc
+ '[' -n /bin/bash ']'
+ hash -r
+ __git_ps1 '\[\e[0;32m\]\[\e[0;33m\]\w' '\[\e[1;32m\] \$ \[\e[m\]'
+ local exit=0
+ local pcmode=no
+ local detached=no
+ local 'ps1pc_start=\u@\h:\w '
+ local 'ps1pc_end=\$ '
+ local 'printf_format= (%s)'
+ case "$#" in
+ pcmode=yes
+ ps1pc_start='\[\e[0;32m\]\[\e[0;33m\]\w'
+ ps1pc_end='\[\e[1;32m\] \$ \[\e[m\]'
+ printf_format=' (%s)'
+ PS1='\[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'
+ local ps1_expanded=yes
+ '[' -z '' ']'
+ '[' -z '3.2.57(1)-release' ']'
+ shopt -q promptvars
+ local repo_info rev_parse_exit_code
+ git rev-parse --git-dir --is-inside-git-dir --is-bare-repository --is-inside-work-tree --short HEAD
+ repo_info=
+ rev_parse_exit_code=128
+ '[' -z '' ']'
+ return 0

3 个答案:

答案 0 :(得分:2)

在您.bash_profile呼叫gitprompt verbose的情况下,请设置:

PROMPT_COMMAND='__git_ps1 "$PS1_INFO" "$PS1_PROMPT"'

我不知道__git_ps1是什么,但是从set -x; source ...的输出中我可以看出它是罪魁祸首:

+ hash -r  # <<< This is the last command executed by the 'activate' script
+ __git_ps1 '\[\e[0;32m\]\[\e[0;33m\]\w' '\[\e[1;32m\] \$ \[\e[m\]'  # <<< This is PROMPT_COMMAND being called
...
+ PS1='\[\e[0;32m\]\[\e[0;33m\]\w\[\e[1;32m\] \$ \[\e[m\]'  # <<< This is PS1 getting restored
...

这里有几个选项,请选择您喜欢的选项:

  • 修改activate并添加一行以取消设置PROMPT_COMMAND;
  • 修改__git_ps1(如果可以),以便在PS1设置时不会触及VIRTUAL_ENV;
  • 如果您无法修改__git_ps1,请在其周围写一个包装器;
  • 在输入virtualenv之前手动取消PROMPT_COMMAND;
  • 避免在gitprompt
  • 中致电.bash_profile

答案 1 :(得分:0)

将@orome和@Andrea放在一起讨论:

我在bash脚本中添加了以下内容:

__venv_prefix()
{
    printf "$CONDA_PROMPT_MODIFIER"
}

# git color hint branch name
source ~/git-prompt.sh
export PROMPT_COMMAND='__venv_prefix; __git_ps1 "\u@\h:\w" "\\\$ "'
export GIT_PS1_SHOWCOLORHINTS=1

激活时显示conda env前缀,在git repo中显示git分支后缀,并且当同时满足两个条件时显示conda前缀和git后缀。

编辑:上面的配置有一个问题,它确实在制表符完成时显示了conda环境。下面的配置没有问题:

parse_git_branch() 
{
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h\[\033[01;34m\] \w\[\033[01;33m\]\$(parse_git_branch)\[\033[01;34m\] \$\[\033[0m\] "

答案 2 :(得分:0)

我遇到了同样的问题,这很烦人,因此在看到Andrea Corbellini的答案后,我在.bash_aliases中添加了以下别名:

alias act="unset PROMPT_COMMAND && source ./.venv/bin/activate" 
alias dact="deactivate && export PROMPT_COMMAND=prompt_command"

我总是使用相同的目录名(.venv),所以这对我来说很好。希望对别人有帮助。