virtualenv name not show in zsh prompt

BashZshOh My-Zsh

Bash Problem Overview


Recently, I give a try on oh my zsh, everything looks good till I try virtualevn and virtualenvwrapper. When I activate a virtualenv (e.g test), on normal bash, I will see the virtualenv name like:

(test)abc@abc:

But when I switched to zsh, I cannot see virtualenv name. Even though, I alr add virtualenv and virtualenvwrapper in plugins of oh my zsh. I also checked the activate file of my virtualenv, it contains:

f [ -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

Is it because the comparision ["x" != x] return true?

Updated: I tried to echo $PS1 in activate file, and got this:

(test) %{$fg[magenta]%}%n%{$reset_color%}%{$fg[cyan]%}@%{$reset_color%}%{$fg[yellow]%}%m%{$reset_color%}%{$fg[red]%}:%{$reset_color%}%{$fg[cyan]%}%0~%{$reset_color%}%{$fg[red]%}|%{$reset_color%}%{$fg[cyan]%}⇒%{$reset_color%}

It seems the $PS1 is correct, but when I echo $PS1 in the terminal, the (test) is gone. It seems the $PS1 is override by something else!

Bash Solutions


Solution 1 - Bash

Do this in ~/.zshrc:

plugins=(virtualenv)

POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)

Caveats:

1 -- add that plugin in addition to other plugins you have.

2 -- I'm using the POWERLEVEL9K theme. Maybe you theme

Solution 2 - Bash

The best solution is to add the following to the end of your ~/.zshrc file:

export VIRTUAL_ENV_DISABLE_PROMPT=

This will override the value in virtualenv.plugin.zsh - no need to change that file.

Solution 3 - Bash

My setting to display Python virtualenv name for the default (a.k.a. robbyrussell) theme is the following.

In the .zshrc file

  1. virtualenv added in plugins

  2. Add custom function:

    function virtualenv_info { 
        [ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
    }
    

Navigate to your theme

  1. My theme is the default theme for zsh:
    $ vim ~/.oh-my-zsh/themes/robbyrussell.zsh-theme
    
  2. Add this command right after existing PROMPT commands:
    PROMPT+='%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%'
    

Finally

$ source ~/.zshrc

PS: You can add your name or a few space before or after the PROMPT+.

Hope that helps!

Solution 4 - Bash

Found the problem, it's due to the theme. The theme I used in the above case is pygmalion, it won't allow u to change $PS1.

After changed to robbyrussell theme, I can change $PS1 in terminal, but still cannot see the virtualenv name. After a while debugging, I found that by default the virtualenv plugin of oh my zsh disable the prompt:

# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1

So just comment out the line in virtualenv plugin, problem solved.

Solution 5 - Bash

As per this guide here

  • First add virtualenv dependency under plugin in file .zshrc If this doesn't work for you, then it means that the theme(one of oh-my-zsh theme) you have selected doesn't include virtualenv name in bash prompt so try second step.

  • Go to file ~/.oh-my-zsh/themes/YOUR_THEME_NAME.zsh-theme and add this in base prompt

%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%

NOTE: virtualenv_prompt_info is the name of function which is declared in ~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh. If your plugin file have different function name then change it accordingly.

Or you can declare your own function in ~/.zshrc file as shown in this guide

Solution 6 - Bash

If you are using conda to start your virtual environment the envorionment variable will be different. To figure out the name of the environment that holds your virtaulenv name type printenv and look through the output. For me it is CONDA_PROMPT_MODIFIER

after you know the name of the variable open .zshrc and add this function

 [ $CONDA_PROMPT_MODIFIER ] && echo `basename $CONDA_PROMPT_MODIFIER`
}```

and below that add this line

`PROMPT="%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%${PROMPT}"`

close the editor and type `source .zshrc`

Solution 7 - Bash

In the case you installed Anaconda using Homebrew:

brew tap homebrew/cask-versions 
brew cask install anaconda 

And you are using POWERLEVEL9K theme

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

All you need to do is add this line to the end of .zshrc :

POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time anaconda)

There's no need for virtualenv plugin.

Edited:

In case you already had conda installed for bash and you get:

zsh: command not found: conda

Run this:

~/anaconda3/bin/conda init zsh

Solution 8 - Bash

I made it work following this link: https://askubuntu.com/a/387098

I reproduce the answer below.

How the prompt is changed is defined in the script bin/activate inside the virtual environment directory. This file is created by virtualenv from a template. Unfortunatelly, the only way of prompt modification provided by the template is prepending (env name) or whatever is set with --prompt.

To modify the prompt in the way you want, I'd suggest circumventing the setting of the prompt in bin/activate and modify the definition of PROMPT in your theme file.

First add the following to your.zsh-theme (or .zshrc)

export VIRTUAL_ENV_DISABLE_PROMPT=yes

function virtenv_indicator {
    if [[ -z $VIRTUAL_ENV ]] then
        psvar[1]=''
    else
        psvar[1]=${VIRTUAL_ENV##*/}
    fi
}

add-zsh-hook precmd virtenv_indicator

and add %(1V.(%1v).) in front of the second line of the definition of PROMPT. It should then look like this:

PROMPT='
%(1V.(%1v).)%{$fg_bold[grey]%}[%{$reset_color%}%{$fg_bold[${host_color}]%}%n@%m%{$reset_color%}%{$fg_bold[grey]%}]%{$reset_color%} %{$fg_bold[blue]%}%10c%{$reset_color%} $(git_prompt_info) $(git_remote_status)
%{$fg_bold[cyan]%}❯%{$reset_color%} '

If you want some color you could add %(1V.%{$fs_bold[yellow]%}(%1v)%{$reset_color%}.) for example.

Explanation:

virtenv_indicator will be called each time before the prompt is created. It checks if $VIRTUAL_ENV is set and not empty. If so, it sets the first element of the $psvar array to $VIRTUAL_ENV with everything before and including the last / removed (like basename $VIRTUAL_ENV but less expensive)

In the definition of PROMPT %(1V.(%1v).) checks if the first element of $psvar is set and not empty (%(1V.true-text.false-text)) and adds the content of the this element plus some parentheses ((%1v))

export VIRTUAL_ENV_DISABLE_PROMPT=yes disables any prompt setting by bin/activate scripts.

Solution 9 - Bash

I am also using Oh My Zsh with the pygmalion theme. I had to edit the pygmalion script to add the virtual environment name before the prompt name.

  • open ~/.oh-my-zsh/themes/pygmalion.zsh-theme, modify the prompt_pygmalion_precmd function as following:
prompt_pygmalion_precmd(){
  setopt localoptions extendedglob

  local gitinfo=$(git_prompt_info)
  local gitinfo_nocolor=${gitinfo//\%\{[^\}]##\}}
  local exp_nocolor="$(print -P \"$base_prompt_nocolor$gitinfo_nocolor$post_prompt_nocolor\")"
  local prompt_length=${#exp_nocolor}
  local python_venv="($(echo $CONDA_DEFAULT_ENV)) "

  PROMPT="${python_venv}${base_prompt}${gitinfo}${post_prompt}"
}

Solution 10 - Bash

I am using oh-my-zsh pygmalion them, and this works for me:

  • add virtualenv plugin in ~/.zshrc

  • open ~/.oh-my-zsh/themes/pygmalion.zsh-theme, modify the prompt_pygmalion_precmd function to this:

     prompt_pygmalion_precmd(){
       setopt localoptions extendedglob
    
       local gitinfo=$(git_prompt_info)
       local gitinfo_nocolor=${gitinfo//\%\{[^\}]##\}}
       local exp_nocolor="$(print -P \"$base_prompt_nocolor$gitinfo_nocolor$post_prompt_nocolor\")"
       local prompt_length=${#exp_nocolor}
       local python_venv=$(virtualenv_prompt_info)
    
       PROMPT="${python_venv}${base_prompt}${gitinfo}${post_prompt}"
     }
    

Basically just add $(virtualenv_prompt_info) to your PROMPT to wherever you prefer, here I added it to the very beginning of my PROMPT.

Solution 11 - Bash

if you use zsh and pyenv, put this into ~/.zshrc

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
#export PS1='($(pyenv version-name)) '$PS1

export PYENV_VIRTUALENV_DISABLE_PROMPT=1
export BASE_PROMPT=$PS1
function updatePrompt {
    if [[ "$(pyenv version-name)" != "system" ]]; then
        # the next line should be double quote; single quote would not work for me
        export PS1="($(pyenv version-name)) "$BASE_PROMPT
    else
        export PS1=$BASE_PROMPT
    fi
}
export PROMPT_COMMAND='updatePrompt'
precmd() { eval '$PROMPT_COMMAND' } # this line is necessary for zsh

Solution 12 - Bash

You do not need to create new function, as per documentation - this function is created for you. https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/virtualenv

But You still need to edit theme file, as mentioned above, just enter correct function name - virtualenv_prompt_info:

PROMPT+='%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%'

Solution 13 - Bash

After playing with the surround answers, I found the following to be the best for my use case. This checks the $VIRTUAL_ENV_PROMPT and $VIRTUAL_ENV variables every time you change directories and sets the prompt with the correct venv info.

DEFAULT_PROMPT=$PROMPT

function cd() {
  builtin cd "$@"

  if [[ -n "$VIRTUAL_ENV_PROMPT" ]] ; then
      PROMPT="$VIRTUAL_ENV_PROMPT$DEFAULT_PROMPT"
  elif [[ -n "$VIRTUAL_ENV" ]] ; then
      PROMPT="(`basename $VIRTUAL_ENV`) $DEFAULT_PROMPT"
  else
      PROMPT=$DEFAULT_PROMPT
  fi
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOhMyGoshView Question on Stackoverflow
Solution 1 - BashWilliam EntrikenView Answer on Stackoverflow
Solution 2 - BashGregView Answer on Stackoverflow
Solution 3 - BashOzkan SerttasView Answer on Stackoverflow
Solution 4 - BashOhMyGoshView Answer on Stackoverflow
Solution 5 - BashYashashviView Answer on Stackoverflow
Solution 6 - BashBrian McCallView Answer on Stackoverflow
Solution 7 - BashIlan HarelView Answer on Stackoverflow
Solution 8 - BashLaboView Answer on Stackoverflow
Solution 9 - BashFabricio Batista NarcizoView Answer on Stackoverflow
Solution 10 - BashTikiLovesSunshineView Answer on Stackoverflow
Solution 11 - BashkaiView Answer on Stackoverflow
Solution 12 - BashPauls BebrisView Answer on Stackoverflow
Solution 13 - Bashkr1tzyView Answer on Stackoverflow