Home

Mysterious problem with missing name of the active virtualenv

Recently I have ran into an issue related to virtualenv and my shell. It did not display name of the active virtualenv after invoking the standard command:

source bin/activate

However, everything works perfectly without that (even a deactivate command). This little thing makes me nervous so I have started looking for a solution to that problem.

After a while I found the reason why it does not work:

1 function calculate_prompt() {
2   # Beautifying the prompt ;).
3 }
4 
5 PROMPT_COMMAND=calculate_prompt

Yeah, I totally forgot about the fact that I am using PROMPT_COMMAND. Right now, when I know the reason, the solution is a piece of cake - just add the code for displaying virtualenv name to the function which calculates the prompt:

 1 function calculate_prompt() {
 2   # Beautifying the prompt ;).
 3   # ...
 4 
 5   if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
 6     _OLD_VIRTUAL_PS1="$PS1"
 7 
 8     if [ "x" != x ] ; then
 9         PS1="$PS1"
10     else
11       if [ ! -z "$VIRTUAL_ENV" ] ; then
12         if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
13           PS1="`basename \`dirname \"$VIRTUAL_ENV\"\``: $PS1"
14         else
15           PS1="`basename $VIRTUAL_ENV`: $PS1"
16         fi
17       fi
18     fi
19   fi
20 }
21 
22 PROMPT_COMMAND=calculate_prompt

And it is fixed! :wink:

Home