pythonbashvirtualenvcommand-promptvirtualenvwrapper

Why doesn't my prompt change when I activate a virtualenv?


When I activate a Python virtualenv with workon, my prompt does not change to identify the environment.

I've confirmed that my activate script contains

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

and that VIRTUAL_ENV_DISABLE_PROMPT is unset.

Why would this be happening and what can I do to fix it?


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


I've got some fancy footwork going on in my .bash_profile, but I don't see why any of this should matter once the prompt has been set:

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 gives:

+ __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

Solution

  • In your .bash_profile you are calling gitprompt verbose that in turn is setting:

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

    I don't know what __git_ps1 is, but from the output of set -x; source ... I can tell that it is the culprit:

    + 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
    ...
    

    Here you have a few options, choose the one you prefer: