How do I change the default virtualenv prompt?

kevin picture kevin · May 2, 2012 · Viewed 20.3k times · Source

How do you change the default Virtualenvwrapper prompt? By default, working on a particular virtual environment with a command like "workon <_name_of_env_>" prepends the name of the virtualenv to your prompt. This may work poorly if you're not using a default command prompt.

Answer

ivanalejandro0 picture ivanalejandro0 · Nov 17, 2013

If you are working on a custom PS1 (as I when found out this issue), I recommend you to disable prompt change, use export VIRTUAL_ENV_DISABLE_PROMPT=1 (see virtualenv docs), and make your own virtualenv prompt in order to add to your PS1.

See this snippet that I've used:

function virtualenv_info(){
    # Get Virtual Env
    if [[ -n "$VIRTUAL_ENV" ]]; then
        # Strip out the path and just leave the env name
        venv="${VIRTUAL_ENV##*/}"
    else
        # In case you don't have one activated
        venv=''
    fi
    [[ -n "$venv" ]] && echo "(venv:$venv) "
}

# disable the default virtualenv prompt change
export VIRTUAL_ENV_DISABLE_PROMPT=1

VENV="\$(virtualenv_info)";
# the '...' are for irrelevant info here.
export PS1="... ${VENV} ..."