Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtualenv name not show in zsh prompt

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!

like image 337
OhMyGosh Avatar asked Aug 13 '16 02:08

OhMyGosh


People also ask

Should I use VENV or Virtualenv?

If you need the additional features that virtualenv provides over venv, then you obviously should use virtualenv. If you're satisfied with your current setup with venv, then there's no reason to choose virtualenv.

How do I find Virtualenv name?

Shell's prompt Inside your virtualenv environment is a file, bin/activate . You can edit this file to change your prompt to whatever you want it to look like. Specifically this section of the file: ... else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi ...

How do I know if Virtualenv is activated?

Check the $VIRTUAL_ENV environment variable. The $VIRTUAL_ENV environment variable contains the virtual environment's directory when in an active virtual environment. Once you run deactivate / leave the virtual environment, the $VIRTUAL_ENV variable will be cleared/empty.

How do I add virtualenv to a zsh plugin?

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.

How do I change the prompt in virtualenv?

Inside your virtualenv environment is a file, bin/activate. You can edit this file to change your prompt to whatever you want it to look like. Specifically this section of the file: ... else PS1=" (`basename "$VIRTUAL_ENV"`)$PS1" fi ... The variable PS1 is a special variable that controls what a shell's prompt will look like.

How to display Python virtualenv name for default theme?

My setting to display Python virtualenv name for the default (a.k.a. robbyrussell) theme is the following. function virtualenv_info { [ $VIRTUAL_ENV ] && echo ' ('`basename $VIRTUAL_ENV`') ' } PROMPT+='% {$fg [green]%}$ (virtualenv_info)% {$reset_color%}%' PS: You can add your name or a few space before or after the PROMPT+. Hope that helps!

How do I get the base name of a virtual_env?

If you know the theme that is being used, you can add a function to retrieve the activated VIRTUAL_ENV base name and use it in the PROMPT variable in the *.zsh-theme file located at ~/.oh-my-zsh/themes/ and that should do the trick! For me it was the default 'oh-my-zsh' theme i.e. 'robbyrussels' which was configured.


1 Answers

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

like image 152
William Entriken Avatar answered Nov 14 '22 02:11

William Entriken