Custom venv prompt not updating when activating virtual environment

43 Views Asked by At

I have tried to customize my virtual environment prompt by adding the following to my .zshrc file:

...
export VIRTUAL_ENV_DISABLE_PROMPT=1
if [[ -v VIRTUAL_ENV ]]; then
    PROMPT='%F{green}(venv)%f'$PROMPT
fi
...

This only works if i $ source ~/.zshrc after activating a virtual environment.

I made it work automatically by adding the aliases below, but I was wondering if there is a better way to make this work automatically?

alias venv='source ./venv/bin/activate; source ~/.zshrc'
alias deactivate='deactivate; source ~/.zshrc'
1

There are 1 best solutions below

0
user1934428 On

Perhaps a simple way for your case would be to define the prompt as

vprompt='%F{green}(venv)%f' # Prefix for venv prompt
PROMPT='${VIRTUAL_ENV+$vprompt}'$PROMPT

If VIRTUAL_ENV is set during evaluation of the prompt, the + causes $vprompt to be printed in front of your original prompt. If VIRTUAL_ENV is unset, nothing is printed, and you only get your original prompt.