pyenv.plugin.zsh 3.0 KB

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