Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change python virtualenv name, shown in prompt?

Is there any way to change name, that is shown in prompt, while virtualenv activated?

username@host:~$ python3 -m venv venv
username@host:~$ source venv/bin/activate
(venv) username@host:~$

But i need it to be shown as something like this:

username@host:~$ python3 -m venv venv
username@host:~$ source venv/bin/activate
(some_arbitrary_name) username@host:~$
like image 684
AlekseyP Avatar asked Jan 28 '26 10:01

AlekseyP


2 Answers

Originally part of the question, written by AlekseyP; removed to form a Community Wiki answer


Prompt name can not be changed after creation, however, there is a way to show some arbitrary name in prompt other than folder name:

username@host:~$ python3 -m venv .venv --prompt some_arbitrary_name
username@host:~$ source venv/bin/activate
(some_arbitrary_name) username@host:~$
like image 140
Charles Duffy Avatar answered Jan 31 '26 01:01

Charles Duffy


The prompt is modified in the bin/activate script, so just change the line that adds (venv). It looks something like this:

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1:-}"
    PS1="(venv) ${PS1:-}"
    export PS1
fi

Just change that (venv) to your name of choice.

Note that in more recent venv versions the above code looks like this:

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

You can still just replace

(`basename \"$VIRTUAL_ENV\"`)

with a simple string like (my_venv)

like image 34
Geoff Genz Avatar answered Jan 31 '26 00:01

Geoff Genz