Format python virtual environment name with shell variable PS1 on bash prompt

I'm trying to customize the bash prompt with the shell variable PS1. How can I access the python virtual environment name to apply color formatting?

PS1 is set to: \u@\h in \W \$

I would expect the output to be user@host in ~ $

But I get (base) user@host in ~ $ ((base) (venv) user@host in ~ $ when using a virtual environment named venv)

If possible it would also be great to only display (venv) instead of (base) (venv), or is there any use case where the (base)-additon makes sense?


Solution 1:

The activate script makes a one-time change of hard-coding the name of the virtual environment into the current value of PS1. You can disable this by adding VIRTUAL_ENV_DISABLE_PROMPT to your environment (any non-empty value will do), and use the value of $(basename $VIRTUAL_ENV) to customize your prompt however you like.