Parent directory

pyenv.plugin.zsh

3562 bytes
  1# if there is a virtualenv already loaded pyenv should not be loaded
  2# see https://github.com/ohmyzsh/ohmyzsh/issues/12589
  3[[ -n ${VIRTUAL_ENV:-} ]] && return
  4
  5pyenv_config_warning() {
  6  [[ "$ZSH_PYENV_QUIET" != true ]] || return 0
  7
  8  local reason="$1"
  9  local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
 10  cat >&2 <<EOF
 11Found pyenv, but it is badly configured ($reason). pyenv might not
 12work correctly for non-interactive shells (for example, when run from a script).
 13${(%):-"%B%F{yellow}"}
 14To fix this message, add these lines to the '.profile' and '.zprofile' files
 15in your home directory:
 16${(%):-"%f"}
 17export PYENV_ROOT="$pyenv_root"
 18export PATH="\$PYENV_ROOT/bin:\$PATH"
 19eval "\$(pyenv init --path)"
 20${(%):-"%F{yellow}"}
 21You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
 22For more information go to https://github.com/pyenv/pyenv/#installation.
 23EOF
 24}
 25
 26# This plugin loads pyenv into the current shell and provides prompt info via
 27# the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
 28
 29# Look for pyenv in $PATH and verify that it's not a part of pyenv-win in WSL
 30if ! command -v pyenv &>/dev/null; then
 31  FOUND_PYENV=0
 32elif [[ "${commands[pyenv]}" = */pyenv-win/* && "$(uname -r)" = *icrosoft* ]]; then
 33  FOUND_PYENV=0
 34else
 35  FOUND_PYENV=1
 36fi
 37
 38# Look for pyenv and try to load it (will only work on interactive shells)
 39if [[ $FOUND_PYENV -ne 1 ]]; then
 40  pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
 41  for dir in $pyenvdirs; do
 42    if [[ -d "$dir/bin" ]]; then
 43      FOUND_PYENV=1
 44      break
 45    fi
 46  done
 47
 48  if [[ $FOUND_PYENV -ne 1 ]]; then
 49    if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
 50      if [[ -d "$dir/bin" ]]; then
 51        FOUND_PYENV=1
 52      fi
 53    fi
 54  fi
 55
 56  # If we found pyenv, load it but show a caveat about non-interactive shells
 57  if [[ $FOUND_PYENV -eq 1 ]]; then
 58    # Configuring in .zshrc only makes pyenv available for interactive shells
 59    export PYENV_ROOT="$dir"
 60    export PATH="$PYENV_ROOT/bin:$PATH"
 61    eval "$(pyenv init --path)"
 62
 63    # Show warning due to bad pyenv configuration
 64    pyenv_config_warning 'pyenv command not found in $PATH'
 65  fi
 66fi
 67
 68if [[ $FOUND_PYENV -eq 1 ]]; then
 69  if [[ -z "$PYENV_ROOT" ]]; then
 70    # This is only for backwards compatibility with users that previously relied
 71    # on this plugin exporting it. pyenv itself does not require it to be exported
 72    export PYENV_ROOT="$(pyenv root)"
 73  fi
 74
 75  # Add pyenv shims to $PATH if not already added
 76  if [[ -z "${path[(Re)$(pyenv root)/shims]}" ]]; then
 77    eval "$(pyenv init --path)"
 78    pyenv_config_warning 'missing pyenv shims in $PATH'
 79  fi
 80
 81  # Load pyenv
 82  eval "$(pyenv init - --no-rehash zsh)"
 83
 84  # If pyenv-virtualenv exists, load it
 85  if [[ "$ZSH_PYENV_VIRTUALENV" != false && "$(pyenv commands)" =~ "virtualenv-init" ]]; then
 86    eval "$(pyenv virtualenv-init - zsh)"
 87  fi
 88
 89  function pyenv_prompt_info() {
 90    local version="$(pyenv version-name)"
 91    if [[ "$ZSH_THEME_PYENV_NO_SYSTEM" == "true" ]] && [[ "${version}" == "system" ]]; then
 92      return
 93    fi
 94    echo "${ZSH_THEME_PYENV_PREFIX=}${version:gs/%/%%}${ZSH_THEME_PYENV_SUFFIX=}"
 95  }
 96else
 97  # Fall back to system python
 98  function pyenv_prompt_info() {
 99    if [[ "$ZSH_THEME_PYENV_NO_SYSTEM" == "true" ]]; then
100      return
101    fi
102    local version="$(python3 -V 2>&1 | cut -d' ' -f2)"
103    echo "${ZSH_THEME_PYENV_PREFIX=}system: ${version:gs/%/%%}${ZSH_THEME_PYENV_SUFFIX=}"
104  }
105fi
106
107unset FOUND_PYENV pyenvdirs dir
108unfunction pyenv_config_warning