pyenv.plugin.zsh 3.5 KB

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