Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is virtualenv not setting my terminal prompt?

I have virtualenv running on Mac OSX (10.8.2), and whilst it works (I can set up venvs, switch between them, activate and deactvate), the one thing that doesn't work (and is quite annoying) is the terminal prompt switch.

My basic prompt is [\u] \w \n\[\033[0;31m\]$\[\e[0m\], which renders as:

[hugo] /current/directory/path/
$ 

i.e. it has a line break in it.

If I activate a virtualenv, I would expect:

(myproject)[hugo] /current/directory/path/
$ 

But in fact I get no change at all.

I've opened up the /bin/activate script, and looked at the code:

if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x" != x ] ; then
        PS1="$PS1"
    else
    if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
        # special case for Aspen magic directories
        # see http://www.zetadev.com/software/aspen/
        PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
    fi
    fi
    export PS1
fi

If I stick an echo in at the end, I can see the that the PS1 var is set to (myproject)[\u] \w \n\[\033[0;31m\]$\[\e[0m\], but for some reason that isn't sticking, and the correct PS1 is not being exported.

Is this something to do with my custom prompt, or a permissions issue? It's clearly not a virtualenv thing, as I know that it works (and in fact it works on my vagrant VM, just not on my OSX host).

?

[UPDATE 1]

My mistake - it looks like I have a git-aware prompt which is overwriting the prompt depending on the directory / repo status. This is what's killing the venv prompt. Now I just need one that does both - so, follow-up question is: does anyone have a colour, git-aware, OSX prompt that places nicely with virtualenv.

like image 903
Hugo Rodger-Brown Avatar asked Feb 20 '13 18:02

Hugo Rodger-Brown


People also ask

How do I activate virtualenv activation?

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

How do I know if virtualenv is activated?

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.


1 Answers

Whatever is setting up your git-aware prompt is probably defining the PROMPT_COMMAND function. Try adding this to the end of your .bashrc file.

add_venv_info () {
    if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
        _OLD_VIRTUAL_PS1="$PS1"

        if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
            # special case for Aspen magic directories
            # see http://www.zetadev.com/software/aspen/
            PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
        elif [ "$VIRTUAL_ENV" != "" ]; then
            PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
        fi
    fi
    export PS1
}
PROMPT_COMMAND=add_venv_info

What are Aspen magic directories? I have no idea; this was code copied from an activate script to demonstrate how to use PROMPT_COMMAND to include virtual environment information into your propmt. If they aren't relevant to your situation, you can simply add the PS1=... assignment that you want. The only really important parts are 1) Checking VIRTUAL_ENV_DISABLE_PROMPT and 2) making the desired assignment to PS1.

like image 149
chepner Avatar answered Sep 18 '22 00:09

chepner