#!/usr/bin/env bash # # Summary: Deactivate virtual environment # # Usage: pyenv deactivate # # Deactivate a Python virtual environment. set -e [ -n "$PYENV_DEBUG" ] && set -x if [ -z "${PYENV_ROOT}" ]; then PYENV_ROOT="$(pyenv-root)" fi unset FORCE unset QUIET # Define `before_deactivate` and `after_deactivate` functions that allow # plugin hooks to register a string of code for execution before or # after deactivating a virtualenv. declare -a before_hooks after_hooks before_deactivate() { local hook="$1" before_hooks["${#before_hooks[@]}"]="$hook" } after_deactivate() { local hook="$1" after_hooks["${#after_hooks[@]}"]="$hook" } # Load plugin hooks. OLDIFS="$IFS" IFS=$'\n' scripts=(`pyenv-hooks deactivate`) IFS="$OLDIFS" for script in "${scripts[@]}"; do source "$script"; done while [ $# -gt 0 ]; do case "$1" in "-f" | "--force" ) FORCE=1 ;; "-q" | "--quiet") QUIET=1 ;; "-v" | "--verbose" ) unset QUIET PYENV_VIRTUALENV_VERBOSE_ACTIVATE=1 ;; * ) break ;; esac shift 1 done if [ -z "${VIRTUAL_ENV}" ]; then if [ -z "${FORCE}" ]; then if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2 fi echo "false" exit 1 fi fi shell="$(basename "${PYENV_SHELL:-$SHELL}")" prefix="${VIRTUAL_ENV}" if [[ "${prefix%/*/envs/*}" == "${PYENV_ROOT}/versions" ]]; then venv="${prefix#${PYENV_ROOT}/versions/}" else venv="${prefix##*/}" fi # Execute `before_deactivate` hooks. for hook in "${before_hooks[@]}"; do eval "$hook"; done if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then echo "pyenv-virtualenv: deactivate ${venv}" 1>&2 fi # conda package anaconda/miniconda scripts (#173) if [ -d "${prefix}/conda-meta" ] || [ -x "${prefix}/bin/conda" ]; then shopt -s nullglob case "${shell}" in fish ) : # conda doesn't support fish ;; * ) for script in "${prefix}/etc/conda/deactivate.d"/*.sh; do echo ". \"${script}\";" done echo "unset CONDA_PREFIX" ;; esac shopt -u nullglob fi if [ -n "${PYENV_ACTIVATE_SHELL}" ]; then # shell version set in pyenv-sh-activate should be unset # https://github.com/yyuu/pyenv-virtualenv/issues/61 case "$shell" in fish ) cat </dev/null 2>&1; then unset -f deactivate; fi; EOS ;; esac # Execute `after_deactivate` hooks. for hook in "${after_hooks[@]}"; do eval "$hook"; done