2018-04-23 02:03:58 +08:00
|
|
|
# This plugin loads pyenv into the current shell and provides prompt info via
|
|
|
|
# the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
|
2013-11-19 16:12:05 +08:00
|
|
|
|
2021-07-26 17:46:15 +08:00
|
|
|
# Look for pyenv in $PATH and verify that it's not a part of pyenv-win in WSL
|
|
|
|
if ! command -v pyenv &>/dev/null; then
|
|
|
|
FOUND_PYENV=0
|
|
|
|
elif [[ "${commands[pyenv]}" = */pyenv-win/* && "$(uname -r)" = *icrosoft* ]]; then
|
2021-07-26 17:45:02 +08:00
|
|
|
FOUND_PYENV=0
|
2021-07-26 17:46:15 +08:00
|
|
|
else
|
|
|
|
FOUND_PYENV=1
|
2020-08-04 21:59:00 +08:00
|
|
|
fi
|
2018-05-15 18:07:23 +08:00
|
|
|
|
2021-07-26 17:45:02 +08:00
|
|
|
# Look for pyenv and try to load it (will only work on interactive shells)
|
2018-05-15 18:07:23 +08:00
|
|
|
if [[ $FOUND_PYENV -ne 1 ]]; then
|
2021-07-26 17:45:02 +08:00
|
|
|
pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
|
|
|
|
for dir in $pyenvdirs; do
|
|
|
|
if [[ -d "$dir/bin" ]]; then
|
|
|
|
FOUND_PYENV=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2018-05-15 18:07:23 +08:00
|
|
|
|
2021-07-26 17:45:02 +08:00
|
|
|
if [[ $FOUND_PYENV -ne 1 ]]; then
|
2018-05-15 18:07:23 +08:00
|
|
|
if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
|
2021-07-26 17:45:02 +08:00
|
|
|
if [[ -d "$dir/bin" ]]; then
|
|
|
|
FOUND_PYENV=1
|
|
|
|
fi
|
2018-05-15 18:07:23 +08:00
|
|
|
fi
|
2021-07-26 17:45:02 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If we found pyenv, load it but show a caveat about non-interactive shells
|
|
|
|
if [[ $FOUND_PYENV -eq 1 ]]; then
|
2021-08-18 17:43:29 +08:00
|
|
|
cat >&2 <<EOF
|
2021-07-26 17:45:02 +08:00
|
|
|
Found pyenv, but it is badly configured. pyenv might not work for
|
|
|
|
non-interactive shells (for example, when run from a script).
|
|
|
|
${bold_color}
|
|
|
|
To fix this message, add these lines to the '.profile' and '.zprofile' files
|
|
|
|
in your home directory:
|
|
|
|
|
|
|
|
export PYENV_ROOT="${dir/#$HOME/\$HOME}"
|
|
|
|
export PATH="\$PYENV_ROOT/bin:\$PATH"
|
|
|
|
eval "\$(pyenv init --path)"
|
2021-08-18 17:43:29 +08:00
|
|
|
|
|
|
|
You'll need to restart your user session for the changes to take effect.${reset_color}
|
2021-07-26 17:45:02 +08:00
|
|
|
For more info go to https://github.com/pyenv/pyenv/#installation.
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Configuring in .zshrc only makes pyenv available for interactive shells
|
2021-08-18 17:47:50 +08:00
|
|
|
export PYENV_ROOT="$dir"
|
2021-07-26 17:45:02 +08:00
|
|
|
export PATH="$PYENV_ROOT/bin:$PATH"
|
|
|
|
eval "$(pyenv init --path)"
|
|
|
|
fi
|
2018-05-15 18:07:23 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $FOUND_PYENV -eq 1 ]]; then
|
2021-08-18 17:47:50 +08:00
|
|
|
if [[ -z "$PYENV_ROOT" ]]; then
|
|
|
|
export PYENV_ROOT="$(pyenv root)"
|
|
|
|
fi
|
|
|
|
|
2021-07-26 17:45:02 +08:00
|
|
|
eval "$(pyenv init - --no-rehash zsh)"
|
|
|
|
|
2021-08-18 17:47:50 +08:00
|
|
|
if [[ -d "$PYENV_ROOT/plugins/pyenv-virtualenv" ]]; then
|
2021-07-26 17:45:02 +08:00
|
|
|
eval "$(pyenv virtualenv-init - zsh)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
function pyenv_prompt_info() {
|
|
|
|
echo "$(pyenv version-name)"
|
|
|
|
}
|
2018-04-23 02:03:58 +08:00
|
|
|
else
|
2021-07-26 17:45:02 +08:00
|
|
|
# Fall back to system python
|
|
|
|
function pyenv_prompt_info() {
|
|
|
|
echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')"
|
|
|
|
}
|
2013-11-19 16:12:05 +08:00
|
|
|
fi
|
2018-05-15 18:07:23 +08:00
|
|
|
|
2019-05-08 15:51:05 +08:00
|
|
|
unset FOUND_PYENV pyenvdirs dir
|