1

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?

coemu
  • 195
  • 1
  • 11

1 Answers1

2

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you, this worked perfect. Searching for the `VIRTUAL_ENV_DISABLE_PROMPT` keyword I also found this very similar question: https://stackoverflow.com/questions/10406926/how-do-i-change-the-default-virtualenv-prompt – coemu Jan 18 '22 at 23:49