16

I'm using virtualenv, virtualenvwrapper, zsh, oh-my-zsh, terminator, on Crunchbang.

I'm trying to display the name of the current virtualenv like so

workon example
(example)... 

I've tried many solutions none seems to work, here's my .zshrc file, I know it's no big deal to fix it but I can't find the right solution. It has been a long time since the last time I used Linux for Django development, I forgot what I used to do.

Right now, I see username@crunchbang, I can't tell which virtualenv I'm using.

  • I believe you're asking how to set your prompt so that it shows which virtualenv you're in, correct? – slm Aug 16 '13 at 23:54
  • @slm yes exactly – Lynob Aug 16 '13 at 23:57
9

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

The variable PS1 is a special variable that controls what a shell's prompt will look like. Changing its value will change your virtualenv prompt:

PS1="(this is my prompt) "

Example

  1. Create a sample environment.

    $ virtualenv tst-env
    
  2. When you're using virtualenv you typically source this file.

    $ cd $HOME/tst-env
    
    $ source bin/activate
    (tst-env)[saml@grinchy tst-env]$ 
    
  3. After making the above change to the variable PS1 in the bin/activate file my prompt is now this:

    $ source bin/activate
    (tst-env)
    

Here are the official instructions on how to do this.

  • tried it now, didn't fix it – Lynob Aug 17 '13 at 9:10
  • here's my activate file pastebin.com/tZqdr1Yz – Lynob Aug 17 '13 at 11:11
  • What happens when you source the activate file? Your file worked fine for me. – slm Aug 17 '13 at 12:32
  • i see no output, so i can't tell what happens, the virtualenv is working other than that i see nothing, maybe it's .zshrc issue or virtualenvwrapper? can this issue be debugged? please note i have 2 virualenv executable files, the normal virtualenv and virtualenv-2.7 is this normal or did virtualenv got downloaded twice? and this is how i installed virtualenv and virualenvwrapper in case it matters sudo pip install virtualenv – Lynob Aug 17 '13 at 13:02
  • @Fischer - I just double checked it using zsh and it works with that too, so this is most likely an env. issue. Let's ignore the 2 virtualenv's setups for the time being. To debug you can enable it, set -x, in your shell and then re-run the source activate command again. You should see PS1= lines where the prompt is getting set. – slm Aug 17 '13 at 14:11
3

If you're using virtualenvwrapper and zsh there are a number of zsh hooks in your ~/.virtualenvs/ directory that you can use to customize your environments. Here is a bit of info regarding these. You can force an update to PS1 that will prepend the current working virtualenv to your shell prompt by adding:

_OLD_VIRTUAL_PS1=$PS1
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
builtin \export PS1

to ~/.virtualenvs/postactivate. To remove the tag when you deactivate, add:

PS1=$_OLD_VIRTUAL_PS1
builtin export PS1

to ~/.virtualenvs/postdeactivate

The only (plausible) thing that should break this is resourcing ~/.zshrc while working in a virtualenv

  • I believe you're postdeactivate script has a mistake. it should read PS1=$_OLD_VIRTUAL_PS1 builtin \export PS1 – Greg Hilston Feb 2 '17 at 16:35
2

You don't have to edit your ~/.zshrc.

Since you are working with virtualenvwrapper it's possible to add options or hooks to $WORKON_HOME/post(de)activate files.

If you want to see more details consult here.

The above link allow me to do next:

In my case $WORKON_HOME=~/Envs because I modified this path when I installed virtualenvwrapper; if you didn't you should have the folder ~/.virtualenvs.

  1. Open the file postactivate located in $WORKON_HOME

  2. Add these lines:

    PS1="$_OLD_VIRTUAL_PS1"
    _OLD_RPROMPT="$RPROMPT"
    RPROMPT="%{${fg_bold[white]}%}(env: %{${fg[green]}%}`basename \"$VIRTUAL_ENV\"`%{${fg_bold[white]}%})%{${reset_color}%} $RPROMPT"
    
  3. Save and enjoy!

    You will obtain something like this: done

  4. (OPTIONAL) If you want you could edit the postdeactivate file to add this line:

    RPROMPT="$_OLD_RPROMPT"
    
0

I just test it on ubuntu 14.04 and i had the same problem. To fix it add this to your .zshrc

source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

or run this in terminal

echo source /usr/share/virtualenvwrapper/virtualenvwrapper.sh >> ~/.zshrc

In Mac OS X I had to do

echo source /usr/local/bin/virtualenvwrapper.sh >> ~/.zshrc
0

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.

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

local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} $(virtualenv_info) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'

Adding the above code generated the below result:

enter image description here

(kunkka.com) is the virtualenv which I just deactivated.

After editing the theme file don't forget to source the .zshrc file.

-> source ~/.zshrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.