#!/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 if [ -z "${PYENV_ROOT}" ]; then PYENV_ROOT="$(pyenv-root)" fi resolve_link() { $(type -p greadlink readlink | head -1) "$1" } unset FORCE unset QUIET # Define `before_activate` and `after_activate` functions that allow # plugin hooks to register a string of code for execution before or # after activating a virtualenv. declare -a before_hooks after_hooks before_activate() { local hook="$1" before_hooks["${#before_hooks[@]}"]="$hook" } after_activate() { local hook="$1" after_hooks["${#after_hooks[@]}"]="$hook" } # Load plugin hooks. OLDIFS="$IFS" IFS=$'\n' scripts=(`pyenv-hooks activate`) IFS="$OLDIFS" for script in "${scripts[@]}"; do source "$script"; done while [ $# -gt 0 ]; do case "$1" in "--complete" ) # Provide pyenv completions echo --unset exec pyenv-virtualenvs --bare ;; "-f" | "--force" ) FORCE=1 ;; "-q" | "--quiet" ) QUIET=1 ;; "--unset" ) exec pyenv-sh-deactivate ;; "-v" | "--verbose" ) unset QUIET PYENV_VIRTUALENV_VERBOSE_ACTIVATE=1 ;; * ) break ;; esac shift 1 done get_current_versions() { local IFS=: current_versions=($(pyenv-version-name 2>/dev/null)) } no_shell= versions=("$@") current_versions=() if [ -z "${versions}" ]; then no_shell=1 get_current_versions versions=("${current_versions[@]}") fi if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then # Backward compatibility issue # https://github.com/yyuu/pyenv-virtualenv/issues/26 no_shell= fi venv="${versions}" if [ -n "${VIRTUAL_ENV}" ]; then # exit as success if a non-pyenv virtualenv is active if [[ -z $PYENV_VIRTUAL_ENV || $PYENV_VIRTUAL_ENV != "$VIRTUAL_ENV" ]]; then if [ -z "${FORCE}" ]; then if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: virtualenv \`${VIRTUAL_ENV}' is already activated" 1>&2 fi echo "true" exit 0 fi fi fi if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then # fallback to virtualenv of current version [ -n "${current_versions}" ] || get_current_versions new_venv="${current_versions%/envs/*}/envs/${venv}" if pyenv-virtualenv-prefix "${new_venv}" 1>/dev/null 2>&1; then venv="${new_venv}" versions[0]="${new_venv}" else if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2 fi echo "false" exit 1 fi 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 [ -L "${prefix}" ]; then prefix="$(resolve_link "${prefix}" 2>/dev/null)" fi # exit as success if the virtualenv is already activated if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then if [ -z "${FORCE}" ]; then if [ -z "${QUIET}" ]; then echo "pyenv-virtualenv: version \`${venv}' is already activated" 1>&2 fi echo "true" exit 0 fi fi pyenv-sh-deactivate --force --quiet || true # Execute `before_activate` hooks. for hook in "${before_hooks[@]}"; do eval "$hook"; done if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then echo "pyenv-virtualenv: activate ${venv}" 1>&2 fi 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 <