pyenv.plugin.zsh 2.8 KB

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