#!/usr/bin/env bash # # Summary: Set or show the shell-specific Python version # # Usage: pyenv shell ... # pyenv shell - # pyenv shell --unset # # Sets a shell-specific Python version by setting the `PYENV_VERSION' # environment variable in your shell. This version overrides local # application-specific versions and the global version. # # should be a string matching a Python version known to pyenv. # The special version string `system' will use your default system Python. # Run `pyenv versions' for a list of available Python versions. # # When `-` is passed instead of the version string, the previously set # version will be restored. With `--unset`, the `PYENV_VERSION` # environment variable gets unset, restoring the environment to the # state before the first `pyenv shell` call. set -e [ -n "$PYENV_DEBUG" ] && set -x # Provide pyenv completions if [ "$1" = "--complete" ]; then echo --unset echo system exec pyenv-versions --bare fi versions=("$@") shell="$(basename "${PYENV_SHELL:-$SHELL}")" if [ -z "$versions" ]; then if [ -z "$PYENV_VERSION" ]; then echo "pyenv: no shell-specific version configured" >&2 exit 1 else echo 'echo "$PYENV_VERSION"' exit fi fi if [ "$versions" = "--unset" ]; then case "$shell" in fish ) echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"' echo "set -e PYENV_VERSION" ;; * ) echo 'PYENV_VERSION_OLD="${PYENV_VERSION-}"' echo "unset PYENV_VERSION" ;; esac exit fi if [ "$versions" = "-" ]; then case "$shell" in fish ) cat <&2 false end EOS ;; * ) cat <&2 false fi EOS ;; esac exit fi # Make sure the specified version is installed. if pyenv-prefix "${versions[@]}" >/dev/null; then OLDIFS="$IFS" IFS=: version="${versions[*]}" IFS="$OLDIFS" if [ "$version" != "$PYENV_VERSION" ]; then case "$shell" in fish ) echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"' echo "set -gx PYENV_VERSION \"$version\"" ;; * ) echo 'PYENV_VERSION_OLD="${PYENV_VERSION-}"' echo "export PYENV_VERSION=\"${version}\"" ;; esac fi else echo "false" exit 1 fi