pyenv.plugin.zsh 2.88 KB
Newer Older
1
pyenv_config_warning() {
2
3
  [[ "$ZSH_PYENV_QUIET" != true ]] || return 0

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  local reason="$1"
  local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
  cat >&2 <<EOF
Found pyenv, but it is badly configured ($reason). pyenv might not
work correctly for non-interactive shells (for example, when run from a script).
${(%):-"%B%F{yellow}"}
To fix this message, add these lines to the '.profile' and '.zprofile' files
in your home directory:
${(%):-"%f"}
export PYENV_ROOT="$pyenv_root"
export PATH="\$PYENV_ROOT/bin:\$PATH"
eval "\$(pyenv init --path)"
${(%):-"%F{yellow}"}
You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
For more information go to https://github.com/pyenv/pyenv/#installation.
EOF
}

22
23
# This plugin loads pyenv into the current shell and provides prompt info via
# the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
Stig Sandbeck Mathisen's avatar
Stig Sandbeck Mathisen committed
24

25
26
27
28
# Look for pyenv in $PATH and verify that it's not a part of pyenv-win in WSL
if ! command -v pyenv &>/dev/null; then
  FOUND_PYENV=0
elif [[ "${commands[pyenv]}" = */pyenv-win/* && "$(uname -r)" = *icrosoft* ]]; then
29
  FOUND_PYENV=0
30
31
else
  FOUND_PYENV=1
32
fi
33

34
# Look for pyenv and try to load it (will only work on interactive shells)
35
if [[ $FOUND_PYENV -ne 1 ]]; then
36
37
38
39
40
41
42
  pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
  for dir in $pyenvdirs; do
    if [[ -d "$dir/bin" ]]; then
      FOUND_PYENV=1
      break
    fi
  done
43

44
  if [[ $FOUND_PYENV -ne 1 ]]; then
45
    if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
46
47
48
      if [[ -d "$dir/bin" ]]; then
        FOUND_PYENV=1
      fi
49
    fi
50
51
52
53
54
  fi

  # If we found pyenv, load it but show a caveat about non-interactive shells
  if [[ $FOUND_PYENV -eq 1 ]]; then
    # Configuring in .zshrc only makes pyenv available for interactive shells
55
    export PYENV_ROOT="$dir"
56
57
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"
58
59
60

    # Show warning due to bad pyenv configuration
    pyenv_config_warning 'pyenv command not found in $PATH'
61
  fi
62
63
64
fi

if [[ $FOUND_PYENV -eq 1 ]]; then
65
  # Setup $PYENV_ROOT if not already set
66
67
  if [[ -z "$PYENV_ROOT" ]]; then
    export PYENV_ROOT="$(pyenv root)"
68
69
70
71
72
73
74
    pyenv_config_warning 'missing $PYENV_ROOT'
  fi

  # Add pyenv shims to $PATH if not already added
  if [[ -z "${path[(Re)$PYENV_ROOT/shims]}" ]]; then
    eval "$(pyenv init --path)"
    pyenv_config_warning 'missing pyenv shims in $PATH'
75
76
  fi

77
  # Load pyenv
78
79
  eval "$(pyenv init - --no-rehash zsh)"

80
  # If pyenv-virtualenv exists, load it
81
  if [[ -d "$PYENV_ROOT/plugins/pyenv-virtualenv" && "$ZSH_PYENV_VIRTUALENV" != false ]]; then
82
83
84
85
86
87
    eval "$(pyenv virtualenv-init - zsh)"
  fi

  function pyenv_prompt_info() {
    echo "$(pyenv version-name)"
  }
88
else
89
90
91
92
  # Fall back to system python
  function pyenv_prompt_info() {
    echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')"
  }
Stig Sandbeck Mathisen's avatar
Stig Sandbeck Mathisen committed
93
fi
94

95
unset FOUND_PYENV pyenvdirs dir
96
unfunction pyenv_config_warning