#!/usr/bin/env bash # Summary: Configure the shell environment for pyenv-virtualenv # Usage: eval "$(pyenv virtualenv-init - [])" # # Automatically activates a Python virtualenv environment based on current # pyenv version. # set -e [ -n "$PYENV_DEBUG" ] && set -x # Detect stat format for mtime: GNU uses -c %Y, BSD uses -f %m # -L follows symlinks: a symlinked .python-version reflects target changes if stat -L -c %Y / >/dev/null 2>&1; then _stat_fmt="-L -c %Y" else _stat_fmt="-L -f %m" fi # Check for version-name hooks at init time. Hooks can alter version # resolution in ways the mtime cache cannot track. If present, the hook # falls back to upstream behavior (no caching). Restart shell after # installing or removing pyenv plugins. _has_version_hooks="" if [ -n "$(pyenv hooks version-name 2>/dev/null)" ]; then _has_version_hooks=1 fi resolve_link() { $(type -p greadlink readlink | head -1) "$1" } abs_dirname() { local cwd="$(pwd)" local path="$1" while [ -n "$path" ]; do cd "${path%/*}" local name="${path##*/}" path="$(resolve_link "$name" || true)" done pwd cd "$cwd" } PYENV_VIRTUALENV_INSTALL_PREFIX="$(dirname "$(abs_dirname "$0")")" print="" for args in "$@" do if [ "$args" = "-" ]; then print=1 shift fi done shell="${1:-$PYENV_SHELL}" if [ -z "$shell" ]; then shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" shell="${shell##-}" shell="${shell%% *}" shell="${shell:-$SHELL}" shell="${shell##*/}" shell="${shell%%-*}" fi if [ -z "$print" ]; then case "$shell" in bash ) profile='~/.bashrc' ;; zsh ) profile='~/.zshrc' ;; ksh ) profile='~/.profile' ;; fish ) profile='~/.config/fish/config.fish' ;; * ) profile='your profile' ;; esac { echo "# Load pyenv-virtualenv automatically by adding" echo "# the following to ${profile}:" echo case "$shell" in fish ) echo 'status --is-interactive; and source (pyenv virtualenv-init -|psub)' ;; * ) echo 'eval "$(pyenv virtualenv-init -)"' ;; esac echo } >&2 exit 1 fi case "$shell" in fish ) cat </dev/null)" = "\$_PYENV_VH_MTIMES" return \$ret end end EOS fi cat </dev/null) EOS fi cat </dev/null)" = "\${_PYENV_VH_MTIMES-}" ]; then return \$ret fi fi EOS fi cat </dev/null)" EOS fi cat <