2024-08-01 20:07:55 +02:00
|
|
|
# if there is a virtualenv already loaded pyenv should not be loaded
|
|
|
|
# see https://github.com/ohmyzsh/ohmyzsh/issues/12589
|
|
|
|
[[ -n ${VIRTUAL_ENV:-} ]] && return
|
|
|
|
|
2021-09-03 12:44:01 +02:00
|
|
|
pyenv_config_warning() {
|
2021-09-03 12:53:33 +02:00
|
|
|
[[ "$ZSH_PYENV_QUIET" != true ]] || return 0
|
|
|
|
|
2021-09-03 12:44:01 +02:00
|
|
|
local reason="$1"
|
|
|
|
local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
|
|
|
|
cat >&2 <<EOF
|
|
|
|
Found pyenv, but it is badly configured ($reason). pyenv might not
|
|
|
|
work correctly for non-interactive shells (for example, when run from a script).
|
|
|
|
${(%):-"%B%F{yellow}"}
|
|
|
|
To fix this message, add these lines to the '.profile' and '.zprofile' files
|
|
|
|
in your home directory:
|
|
|
|
${(%):-"%f"}
|
|
|
|
export PYENV_ROOT="$pyenv_root"
|
|
|
|
export PATH="\$PYENV_ROOT/bin:\$PATH"
|
|
|
|
eval "\$(pyenv init --path)"
|
|
|
|
${(%):-"%F{yellow}"}
|
|
|
|
You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
|
|
|
|
For more information go to https://github.com/pyenv/pyenv/#installation.
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2018-04-22 20:03:58 +02: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 09:12:05 +01:00
|
|
|
|
2021-07-26 11:46:15 +02: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 11:45:02 +02:00
|
|
|
FOUND_PYENV=0
|
2021-07-26 11:46:15 +02:00
|
|
|
else
|
|
|
|
FOUND_PYENV=1
|
2020-08-04 15:59:00 +02:00
|
|
|
fi
|
2018-05-15 12:07:23 +02:00
|
|
|
|
2021-07-26 11:45:02 +02:00
|
|
|
# Look for pyenv and try to load it (will only work on interactive shells)
|
2018-05-15 12:07:23 +02:00
|
|
|
if [[ $FOUND_PYENV -ne 1 ]]; then
|
2021-07-26 11:45:02 +02: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 12:07:23 +02:00
|
|
|
|
2021-07-26 11:45:02 +02:00
|
|
|
if [[ $FOUND_PYENV -ne 1 ]]; then
|
2018-05-15 12:07:23 +02:00
|
|
|
if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
|
2021-07-26 11:45:02 +02:00
|
|
|
if [[ -d "$dir/bin" ]]; then
|
|
|
|
FOUND_PYENV=1
|
|
|
|
fi
|
2018-05-15 12:07:23 +02:00
|
|
|
fi
|
2021-07-26 11:45:02 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If we found pyenv, load it but show a caveat about non-interactive shells
|
|
|
|
if [[ $FOUND_PYENV -eq 1 ]]; then
|
|
|
|
# Configuring in .zshrc only makes pyenv available for interactive shells
|
2021-08-18 11:47:50 +02:00
|
|
|
export PYENV_ROOT="$dir"
|
2021-07-26 11:45:02 +02:00
|
|
|
export PATH="$PYENV_ROOT/bin:$PATH"
|
|
|
|
eval "$(pyenv init --path)"
|
2021-09-03 12:44:01 +02:00
|
|
|
|
|
|
|
# Show warning due to bad pyenv configuration
|
|
|
|
pyenv_config_warning 'pyenv command not found in $PATH'
|
2021-07-26 11:45:02 +02:00
|
|
|
fi
|
2018-05-15 12:07:23 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $FOUND_PYENV -eq 1 ]]; then
|
2021-08-18 11:47:50 +02:00
|
|
|
if [[ -z "$PYENV_ROOT" ]]; then
|
2021-09-06 10:32:19 +02:00
|
|
|
# This is only for backwards compatibility with users that previously relied
|
|
|
|
# on this plugin exporting it. pyenv itself does not require it to be exported
|
2021-08-18 11:47:50 +02:00
|
|
|
export PYENV_ROOT="$(pyenv root)"
|
2021-09-03 12:44:01 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Add pyenv shims to $PATH if not already added
|
2021-09-06 10:32:19 +02:00
|
|
|
if [[ -z "${path[(Re)$(pyenv root)/shims]}" ]]; then
|
2021-09-03 12:44:01 +02:00
|
|
|
eval "$(pyenv init --path)"
|
|
|
|
pyenv_config_warning 'missing pyenv shims in $PATH'
|
2021-08-18 11:47:50 +02:00
|
|
|
fi
|
|
|
|
|
2021-09-03 12:44:01 +02:00
|
|
|
# Load pyenv
|
2021-07-26 11:45:02 +02:00
|
|
|
eval "$(pyenv init - --no-rehash zsh)"
|
|
|
|
|
2021-09-03 12:44:01 +02:00
|
|
|
# If pyenv-virtualenv exists, load it
|
2022-11-14 17:36:19 +01:00
|
|
|
if [[ "$ZSH_PYENV_VIRTUALENV" != false && "$(pyenv commands)" =~ "virtualenv-init" ]]; then
|
2021-07-26 11:45:02 +02:00
|
|
|
eval "$(pyenv virtualenv-init - zsh)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
function pyenv_prompt_info() {
|
2021-12-26 19:32:52 +01:00
|
|
|
local version="$(pyenv version-name)"
|
2024-11-19 20:32:01 +01:00
|
|
|
if [[ "$ZSH_THEME_PYENV_NO_SYSTEM" == "true" ]] && [[ "${version}" == "system" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
echo "${ZSH_THEME_PYENV_PREFIX=}${version:gs/%/%%}${ZSH_THEME_PYENV_SUFFIX=}"
|
2021-07-26 11:45:02 +02:00
|
|
|
}
|
2018-04-22 20:03:58 +02:00
|
|
|
else
|
2021-07-26 11:45:02 +02:00
|
|
|
# Fall back to system python
|
|
|
|
function pyenv_prompt_info() {
|
2024-11-19 20:32:01 +01:00
|
|
|
if [[ "$ZSH_THEME_PYENV_NO_SYSTEM" == "true" ]]; then
|
|
|
|
return
|
|
|
|
fi
|
2022-04-09 14:45:42 +02:00
|
|
|
local version="$(python3 -V 2>&1 | cut -d' ' -f2)"
|
2024-11-19 20:32:01 +01:00
|
|
|
echo "${ZSH_THEME_PYENV_PREFIX=}system: ${version:gs/%/%%}${ZSH_THEME_PYENV_SUFFIX=}"
|
2021-07-26 11:45:02 +02:00
|
|
|
}
|
2013-11-19 09:12:05 +01:00
|
|
|
fi
|
2018-05-15 12:07:23 +02:00
|
|
|
|
2019-05-08 09:51:05 +02:00
|
|
|
unset FOUND_PYENV pyenvdirs dir
|
2021-09-03 12:44:01 +02:00
|
|
|
unfunction pyenv_config_warning
|