Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing to detect -m venv when "python" is not provided by the distro #479

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bin/pyenv-virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
6 changes: 3 additions & 3 deletions test/envs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<OUT
PYENV_VERSION=3.5.1 python3.5 -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1/envs/venv python -s -m ensurepip
rehashed
OUT
Expand Down
2 changes: 1 addition & 1 deletion test/hooks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ OUT
create_executable "3.5.1" "virtualenv"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.5.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.5.1'"
stub pyenv-exec "python3.5 -m venv --help : true"
stub pyenv-exec "python -m venv --help : true"
stub pyenv-hooks "virtualenv : echo '$HOOK_PATH'/virtualenv.bash"
stub pyenv-exec "echo PYENV_VERSION=3.5.1 \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=3.5.1 \"\$@\""
Expand Down
13 changes: 7 additions & 6 deletions test/pip.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ unstub_pyenv() {
stub_pyenv "${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 <<OUT
PYENV_VERSION=3.5.1 python3.5 -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python -m venv ${TMP}/pyenv/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1/envs/venv python -s -m ensurepip
rehashed
OUT
Expand All @@ -52,8 +53,8 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python3.3 -m venv --help : true"
stub pyenv-exec "python3.3 -m venv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";mkdir -p \${PYENV_ROOT}/versions/3.3.6/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.3.6/envs/venv/bin"
stub pyenv-exec "python -s -m ensurepip : false"
stub pyenv-exec "python -s */get-pip.py : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";touch \${PYENV_ROOT}/versions/3.3.6/envs/venv/bin/pip"
stub curl true
Expand All @@ -62,7 +63,7 @@ OUT

assert_success
assert_output <<OUT
PYENV_VERSION=3.3.6 python3.3 -m venv ${PYENV_ROOT}/versions/3.3.6/envs/venv
PYENV_VERSION=3.3.6 python -m venv ${PYENV_ROOT}/versions/3.3.6/envs/venv
Installing pip from https://bootstrap.pypa.io/pip/3.3/get-pip.py...
PYENV_VERSION=3.3.6/envs/venv python -s ${TMP}/pyenv/cache/get-pip.py
rehashed
Expand Down
8 changes: 2 additions & 6 deletions test/python.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ teardown() {
create_executable "2.7.8" "python2.7"
remove_executable "2.7.9" "python2.7"

stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv --verbose * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"
stub pyenv-which "python2.7 : echo ${PYENV_ROOT}/versions/2.7.8/bin/python2.7"

run pyenv-virtualenv --verbose --python=python2.7 venv

unstub pyenv-exec
assert_output <<OUT
PYENV_VERSION=2.7.8 virtualenv --verbose --python=${PYENV_ROOT}/versions/2.7.8/bin/python2.7 ${PYENV_ROOT}/versions/2.7.8/envs/venv
OUT
assert_success

unstub pyenv-which
unstub pyenv-exec

remove_executable "2.7.7" "python2.7"
remove_executable "2.7.8" "python2.7"
Expand All @@ -60,8 +58,6 @@ OUT
remove_executable "2.7.8" "python2.7"
create_executable "2.7.9" "python2.7"

stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv --verbose * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"
Expand Down Expand Up @@ -118,4 +114,4 @@ pyenv-virtualenv: \`99.99.99' is not installed in pyenv.
It does not look like a valid Python version. See \`pyenv install --list' for available versions.
OUT
assert_failure
}
}
16 changes: 6 additions & 10 deletions test/pyvenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ unstub_pyenv() {
stub_pyenv "${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} \"\$@\""
stub pyenv-exec "python -m venv --help : true"
stub pyenv-exec "python -m venv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"

run pyenv-virtualenv venv

assert_output <<OUT
PYENV_VERSION=3.5.1 python3.5 -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
rehashed
OUT
assert [ -x "${PYENV_ROOT}/versions/3.5.1/envs/venv/bin/pydoc" ]
Expand All @@ -52,7 +52,7 @@ OUT
stub_pyenv "${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 "python -m venv --help : true"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"

Expand All @@ -77,8 +77,6 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python3.2 -m venv --help : false"
stub pyenv-exec "python3 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "pip install virtualenv* : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
Expand All @@ -105,7 +103,7 @@ OUT
stub_pyenv "${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 "python -m venv --help : true"
stub pyenv-exec "pip install virtualenv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"
Expand All @@ -132,7 +130,7 @@ OUT
stub_pyenv "${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 "python -m venv --help : true"
stub pyenv-exec "pip install virtualenv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"
Expand All @@ -159,8 +157,6 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python3.2 -m venv --help : false"
stub pyenv-exec "python3 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "pip install virtualenv* : echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "virtualenv * : echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ setup_conda() {
teardown_conda() {
rm -fr "${PYENV_ROOT}/versions/$1"
}

6 changes: 2 additions & 4 deletions test/version.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ setup() {
setup_virtualenv "2.7.7"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/2.7.7'"
stub pyenv-version-name "echo 2.7.7"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv --version : echo \"1.11\""

Expand All @@ -29,12 +27,12 @@ setup() {
setup_m_venv "3.4.1"
stub pyenv-version-name "echo 3.4.1"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.4.1'"
stub pyenv-exec "python3.4 -m venv --help : true"
stub pyenv-exec "python -m venv --help : true"

run pyenv-virtualenv --version

assert_success
[[ "$output" == "pyenv-virtualenv "?.?.?" (python3.4 -m venv)" ]]
[[ "$output" == "pyenv-virtualenv "?.?.?" (python -m venv)" ]]

unstub pyenv-prefix
teardown_m_venv "3.4.1"
Expand Down
12 changes: 0 additions & 12 deletions test/virtualenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ unstub_pyenv() {
export PYENV_VERSION="2.7.11"
stub_pyenv "${PYENV_VERSION}"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"; mkdir -p \"\$2/bin\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -58,8 +56,6 @@ OUT
@test "create virtualenv from a given prefix" {
stub_pyenv "2.7.11"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -89,8 +85,6 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -119,8 +113,6 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -149,8 +141,6 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -214,8 +204,6 @@ OUT
export PYENV_VERSION="2.7.11"
stub_pyenv "${PYENV_VERSION}"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python2.7 -m venv --help : false"
stub pyenv-exec "python2 -m venv --help : false"
stub pyenv-exec "python -m venv --help : false"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down
Loading