pyenv.plugin.zsh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # This plugin loads pyenv into the current shell and provides prompt info via
  2. # the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
  3. # Look for pyenv in $PATH and verify that it's not a part of pyenv-win in WSL
  4. if ! command -v pyenv &>/dev/null; then
  5. FOUND_PYENV=0
  6. elif [[ "${commands[pyenv]}" = */pyenv-win/* && "$(uname -r)" = *icrosoft* ]]; then
  7. FOUND_PYENV=0
  8. else
  9. FOUND_PYENV=1
  10. fi
  11. # Look for pyenv and try to load it (will only work on interactive shells)
  12. if [[ $FOUND_PYENV -ne 1 ]]; then
  13. pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
  14. for dir in $pyenvdirs; do
  15. if [[ -d "$dir/bin" ]]; then
  16. FOUND_PYENV=1
  17. break
  18. fi
  19. done
  20. if [[ $FOUND_PYENV -ne 1 ]]; then
  21. if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
  22. if [[ -d "$dir/bin" ]]; then
  23. FOUND_PYENV=1
  24. fi
  25. fi
  26. fi
  27. # If we found pyenv, load it but show a caveat about non-interactive shells
  28. if [[ $FOUND_PYENV -eq 1 ]]; then
  29. cat <<EOF
  30. Found pyenv, but it is badly configured. pyenv might not work for
  31. non-interactive shells (for example, when run from a script).
  32. ${bold_color}
  33. To fix this message, add these lines to the '.profile' and '.zprofile' files
  34. in your home directory:
  35. export PYENV_ROOT="${dir/#$HOME/\$HOME}"
  36. export PATH="\$PYENV_ROOT/bin:\$PATH"
  37. eval "\$(pyenv init --path)"
  38. ${reset_color}
  39. For more info go to https://github.com/pyenv/pyenv/#installation.
  40. EOF
  41. # Configuring in .zshrc only makes pyenv available for interactive shells
  42. export PYENV_ROOT=$dir
  43. export PATH="$PYENV_ROOT/bin:$PATH"
  44. eval "$(pyenv init --path)"
  45. fi
  46. fi
  47. if [[ $FOUND_PYENV -eq 1 ]]; then
  48. eval "$(pyenv init - --no-rehash zsh)"
  49. if (( ${+commands[pyenv-virtualenv-init]} )); then
  50. eval "$(pyenv virtualenv-init - zsh)"
  51. fi
  52. function pyenv_prompt_info() {
  53. echo "$(pyenv version-name)"
  54. }
  55. else
  56. # Fall back to system python
  57. function pyenv_prompt_info() {
  58. echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')"
  59. }
  60. fi
  61. unset FOUND_PYENV pyenvdirs dir