From efd0d75b5cb16fbadb72805cc57d6ae8561c1ac4 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Wed, 10 Apr 2024 00:03:07 +0300 Subject: [PATCH] Fix failing to detect `-m venv` when "python" is not provided by the distro --- bin/pyenv-virtualenv | 12 ++++++++++-- test/envs.bats | 6 +++--- test/hooks.bats | 2 +- test/pip.bats | 13 +++++++------ test/python.bats | 8 ++------ test/pyvenv.bats | 16 ++++++---------- test/test_helper.bash | 1 + test/version.bats | 6 ++---- test/virtualenv.bats | 12 ------------ 9 files changed, 32 insertions(+), 44 deletions(-) diff --git a/bin/pyenv-virtualenv b/bin/pyenv-virtualenv index 83164228..f7f391a6 100755 --- a/bin/pyenv-virtualenv +++ b/bin/pyenv-virtualenv @@ -155,9 +155,17 @@ detect_venv() { if [ -x "${prefix}/bin/virtualenv" ]; then HAS_VIRTUALENV=1 fi - # Prefer `python3.x` executable if available (#206, #282) local python - for python in "python${PYENV_VERSION%.*}" "python${PYENV_VERSION%%.*}" "python"; do + local -a pythons + if [[ $PYENV_VERSION == "system" ]]; then + # Prefer `python3.x` executable if available (#206, #282) + pythons=("python3" "python" "python2") + else + # as per PEP 394, custom activated Python environments should provide the "python" command + # this includes Pyenv-provided installations + pythons=("python") + fi + for python in "${pythons[@]}"; do if pyenv-exec "${python}" -m venv --help 1>/dev/null 2>&1; then HAS_M_VENV=1 M_VENV_PYTHON_BIN="${python}" diff --git a/test/envs.bats b/test/envs.bats index 93bc114c..325aa336 100644 --- a/test/envs.bats +++ b/test/envs.bats @@ -25,15 +25,15 @@ unstub_pyenv() { stub pyenv-version-name "echo '${PYENV_VERSION}'" stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'" stub pyenv-virtualenv-prefix " : false" - stub pyenv-exec "python3.5 -m venv --help : true" - stub pyenv-exec "python3.5 -m venv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";mkdir -p \${PYENV_ROOT}/versions/3.5.1/envs/venv/bin" + stub pyenv-exec "python -m venv --help : true" + stub pyenv-exec "python -m venv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";mkdir -p \${PYENV_ROOT}/versions/3.5.1/envs/venv/bin" stub pyenv-exec "python -s -m ensurepip : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";touch \${PYENV_ROOT}/versions/3.5.1/envs/venv/bin/pip" run pyenv-virtualenv venv assert_success assert_output <