VIRTUAL_ENV_DISABLE_PROMPT='1' __prompt_venv_rel() { __prompt_venv_rel_dir="$PWD" __prompt_venv_rel_up='' while [ "${1#$__prompt_venv_rel_dir/}" = "$1" ] do __prompt_venv_rel_dir="${__prompt_venv_rel_dir%/*}" __prompt_venv_rel_up="../$__prompt_venv_rel_up" done printf '%s' "$__prompt_venv_rel_up${1#$__prompt_venv_rel_dir/}" } __prompt_venv() { if [ "${VIRTUAL_ENV:-}" ] then printf "$1" "$(__prompt_venv_rel "$VIRTUAL_ENV")" fi } # Ask to activate the first virtual environment found, searching from the # current directory up to `$HOME` or `/`, whichever comes first. # https://github.com/python/cpython/blob/master/Lib/venv/scripts/common/activate __prompt_venv_activate() { if [ "${VIRTUAL_ENV:-}" ] then deactivate fi __prompt_venv_dir="$PWD" while [ "$__prompt_venv_dir" != "$HOME" ] && [ "$__prompt_venv_dir" != '' ] do for __prompt_venv_activate in \ "$__prompt_venv_dir/"*'/bin/activate' \ "$__prompt_venv_dir/."*'/bin/activate' do [ -r "$__prompt_venv_activate" ] || continue grep -q 'VIRTUAL_ENV' "$__prompt_venv_activate" || continue __prompt_venv="${__prompt_venv_activate%/*/*}" if [ "$__prompt_venv" != "$__prompt_venv_prev" ] then while true do printf 'Activate %s? [y/N] ' "$(__prompt_venv_rel "$__prompt_venv")" read -r __prompt_venv_reply case "$__prompt_venv_reply" in 'y'|'n'|'') break; ;; esac done fi case "$__prompt_venv_reply" in 'y') . "$__prompt_venv_activate"; ;; esac __prompt_venv_prev="$__prompt_venv" return 0 done __prompt_venv_dir="${__prompt_venv_dir%/*}" done }