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
2
3
4
5
function calculate_prompt() {
  # Beautifying the prompt ;).
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function calculate_prompt() {
  # Beautifying the prompt ;).
  # ...

  if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"

    if [ "x" != x ] ; then
        PS1="$PS1"
    else
      if [ ! -z "$VIRTUAL_ENV" ] ; then
        if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
          PS1="`basename \`dirname \"$VIRTUAL_ENV\"\``: $PS1"
        else
          PS1="`basename $VIRTUAL_ENV`: $PS1"
        fi
      fi
    fi
  fi
}

PROMPT_COMMAND=calculate_prompt

And it is fixed! :wink:

Home