Skip to content

Commit

Permalink
Ask pyenv whether pip exists if installed (sorin-ionescu#1848)
Browse files Browse the repository at this point in the history
Pyenv will install shims for commands that exist in any interpreter, even if it is not the current one. This means that a command may technically exist, but when executed will tell the user to try a different interpreter. The original check for pip in the Python module can fail for this reason, in particular on Ubuntu 20.04.

This change checks with pyenv whether pip really exists in the current interpreter to work around this problem and fixes a bug in pip command detection.
  • Loading branch information
jcassee authored and to266 committed May 11, 2021
1 parent 88e9829 commit 05364b4
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions modules/python/init.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,35 @@ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) && \
fi

# Load PIP completion.
if (( $#commands[(i)pip(|[23])] )); then
cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/prezto/pip-cache.zsh"

# Detect and use one available from among 'pip', 'pip2', 'pip3' variants
# Detect and use one available from among 'pip', 'pip2', 'pip3' variants
if [[ -n "$PYENV_ROOT" ]]; then
for pip in pip{,2,3}; do
pip_command="$(pyenv which "$pip" 2>/dev/null)"
[[ -n "$pip_command" ]] && break
done
unset pip
else
pip_command="$commands[(i)pip(|[23])]"
fi
if [[ -n "$pip_command" ]]; then
cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/prezto/pip-cache.zsh"

if [[ "$pip_command" -nt "$cache_file" \
|| "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \
|| ! -s "$cache_file" ]]; then
mkdir -p "$cache_file:h"
# pip is slow; cache its output. And also support 'pip2', 'pip3' variants
$pip_command completion --zsh \
"$pip_command" completion --zsh \
| sed -e "s/\(compctl -K [-_[:alnum:]]* pip\).*/\1{,2,3}{,.{0..9}}/" \
>! "$cache_file" \
2> /dev/null
fi

source "$cache_file"

unset cache_file pip_command
unset cache_file
fi
unset pip_command

# Load conda into the shell session, if requested
zstyle -T ':prezto:module:python' conda-init
Expand Down

0 comments on commit 05364b4

Please sign in to comment.