Skip to content

Instantly share code, notes, and snippets.

@disouzam
Forked from karrtikr/deactivate
Created August 12, 2024 21:02
Show Gist options
  • Save disouzam/80b8c004a6880fecac8395ad7f2a1d49 to your computer and use it in GitHub Desktop.
Save disouzam/80b8c004a6880fecac8395ad7f2a1d49 to your computer and use it in GitHub Desktop.
Script to deactivate virtual environments for bash or zsh
# >>> Virtual env deactivate hook >>>
# Same as deactivate in "<venv>/bin/activate"
deactivate () {
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r 2> /dev/null
fi
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
unset VIRTUAL_ENV_PROMPT
if [ ! "${1:-}" = "nondestructive" ] ; then
unset -f deactivate
fi
}
# Initialize the variables required by deactivate function
_OLD_VIRTUAL_PS1="${PS1:-}"
_OLD_VIRTUAL_PATH="$PATH"
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
fi
# <<< Virtual env deactivate hook <<<
@disouzam
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment