#!/usr/bin/env bash # # Summary: Activate virtual environment # # Usage: pyenv activate # pyenv activate --unset # # Activate a Python virtualenv environment in current shell. # This acts almost as same as `pyenv shell`, but this invokes the `activate` # script in your shell. # # should be a string matching a Python version known to pyenv. set -e [ -n "$PYENV_DEBUG" ] && set -x unset DEACTIVATE unset FORCE unset QUIET unset VERBOSE while [ $# -gt 0 ]; do case "$1" in "--complete" ) # Provide pyenv completions echo --unset exec pyenv-virtualenvs --bare ;; "--deactivate" ) DEACTIVATE=1 ;; "--force" ) FORCE=1 ;; "--no-error" | "--quiet" ) QUIET=1 ;; "--unset" ) echo "pyenv deactivate" exit ;; "--verbose" ) VERBOSE=1 ;; * ) break ;; esac shift 1 done no_shell= versions=("$@") if [ -z "${versions}" ]; then no_shell=1 OLDIFS="$IFS" IFS=: versions=($(pyenv-version-name)) IFS="$OLDIFS" fi if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then # Backward compatibility issue # https://github.com/yyuu/pyenv-virtualenv/issues/26 no_shell= fi venv="${versions}" if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then if [ -z "$QUIET" ]; then echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2 fi echo "false" exit 1 fi # exit as error if there are multiple virtualenvs # https://github.com/yyuu/pyenv-virtualenv/issues/105 for version in "${versions[@]}"; do if [[ "${version}" != "${venv}" ]]; then if pyenv-virtualenv-prefix "${version}" 1>/dev/null 2>&1; then if [ -z "$QUIET" ]; then echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 fi echo "false" exit 1 fi fi done shell="${PYENV_SHELL:-${SHELL##*/}}" prefix="$(pyenv-prefix "${venv}")" if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: version \`${venv}' is already activated" 1>&2 fi echo "true" exit 0 fi if [[ "${PYENV_DEACTIVATE}" == "${prefix}" ]]; then if [ -z "${FORCE}" ]; then if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: \`${venv}' is marked deactivated. use \`pyenv activate --force ${venv}' to activate forcibly." 1>&2 fi echo "false" exit 1 fi fi # Display setup instruction, if pyenv-virtualenv has not been initialized. # if 'pyenv virtualenv-init -' is not found in "$profile" if [ -z "$PYENV_VIRTUALENV_INIT" ]; then pyenv-virtualenv-init >&2 || true fi if [ -n "${DEACTIVATE}" ]; then pyenv-sh-deactivate ${QUIET+--quiet} ${VERBOSE+--verbose} fi echo "pyenv-virtualenv: activate ${venv}" 1>&2 if [ -z "$no_shell" ]; then # shell version set in pyenv-sh-activate should be unset # https://github.com/yyuu/pyenv-virtualenv/issues/61 OLDIFS="$IFS" IFS=: case "$shell" in fish ) cat <