0

I'm in the process of switching from bash to zsh, and I'm having trouble getting Python virtual environment info to display correctly in my prompt. This thread has the most detail, but many of the answers are referring to specific themes, and I'm trying to do it manually.

I copied and slightly modified code from oh-my-zsh and from that thread; here's what I have in my .zshrc:

function virtualenv_prompt_info(){
  [[ -n ${VIRTUAL_ENV} ]] || return
  echo "${ZSH_THEME_VIRTUALENV_PREFIX=[}${VIRTUAL_ENV:t:gs/%/%%}${ZSH_THEME_VIRTUALENV_SUFFIX=]} "
}

# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1

# add virtualenv to prompt
ZSH_THEME_VIRTUALENV_PREFIX='('
ZSH_THEME_VIRTUALENV_SUFFIX=')'
PROMPT='$(virtualenv_prompt_info)'

# (not shown: other additions to the prompt that occur later)

The result of this is that when I am in zsh, in a directory under a virtual environment, and execute source venv/bin/activate, I then get a (venv) at the start of my prompt, as I desire. However, there are two problems: When I leave the directory that has the virtualenv, that (venv) prefix remains in my prompt; and when I enter a directory with a live virtualenv, I don't get the prefix unless I re-run activate. The behavior I desire is that I get the prefix any time I'm in a directory where a virtual environment is active (without having to do anything manually), and I don't get the prefix if I'm not in such a directory.

These both worked correctly with the prompt mangling that's built in to virtualenv, but I'd like to control it with zsh (I may start using a theme later, but for now I want to control everything manually). What am I doing wrong?

user1235777
  • 609
  • 7
  • 24

0 Answers0