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

4

11

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:~$
Pudendas answered 13/1, 2020 at 10:18 Comment(6)
change the name of the venv :)Counter
#43256869Apart
@AlekseyP: I'd encourage you to post your python3 -m venv .venv --prompt some_arbitrary_name solution as an actual answer. None of the existing answers (as of when I'm writing this) mention it, and some readers on this site will jump straight to looking at the answers as soon as they believe the question to be the same as what they're trying to do, and thus might miss your update. (Also, you'll be able to get more reputation on the site, if you care about that.) I happily pledge an upvote on any answer submission from you that closely mirrors your update text.Eastertide
@Eastertide --prompt option works great, thank you!Fontes
@alekseyp Regarding your point "Prompt name can not be changed after creation", I see a prompt option in pyvenv.cfg file, you could try changing it.Fontes
Note also that answers don't belong in questions, and the guidance on Meta Stack Overflow is that when an answer is provided in a question, good practice is to edit to remove it. So the advice to add your own answer as a separate answer instead of relying on people to find it in the question is extremely sound. (Putting an answer in the question itself privileges it over other answers and doesn't allow separate voting/editing/etc distinct from the question, hence this being frowned on as it is).Etherealize
I
5

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)

Integral answered 11/2, 2022 at 22:36 Comment(0)
E
4

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:~$
Etherealize answered 13/1, 2020 at 10:18 Comment(0)
N
1

The venv name is the same as the folder you created it in. Make a venv with a name you like

$ python -m venv some_arbitrary_name
$ . ./some_arbitrary_name/bin/activate
(some_arbitrary_name)$ 
Northumbria answered 13/1, 2020 at 10:21 Comment(1)
Often, this is not the most desirable, for a variety of reasons (mostly convention-oriented, but still... e.g. .gitignore files, aliases to run activate, etc.) -- so there's an alternative: the --prompt option to venv, e.g: python3 -m venv --prompt some_arbitrary_name .env -- will create ./.env, and . ./.env/bin/activate will give the same prompt as for you.Eastertide
I
-1

While that is not possible, I can't think of a scenario where you might want to show a name different than the virtual env name. If you're looking to rename your virtual environment, here's what you could do.
By default virtualenv does not support the renaming of environments. It is safer to just delete the virtualenv directory and create a new one with the correct name. You can do this by:

Activate your virtualenv: source vnev/bin/activate
Create a requirements.txt of currently installed packages: pip freeze > requirements.txt
Delete the misspelled virtualenv: rm -r vnev/
Create a new virtualenv with correct name: virtualenv venv
Activate new virtualenv: source venv/bin/activate
Install packages from requirements.txt: pip install -r requirements.txt
Internal answered 13/1, 2020 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.