Skip to content

Commit

Permalink
Merge pull request #296 from pyenv/yy-py-venv-major-minor
Browse files Browse the repository at this point in the history
 Prefer `python3.x` executable if available
  • Loading branch information
yyuu authored Jan 12, 2019
2 parents bfdeb31 + a9e1891 commit 2d947be
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 24 deletions.
22 changes: 16 additions & 6 deletions bin/pyenv-virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ http_get_wget() {
}

version() {
if [[ -z "${PYENV_VERSION:-}" ]]; then
# `PYENV_VERSION` might not be declared if this was invoked via `--version`
export PYENV_VERSION="$(pyenv-version-name)"
fi
detect_venv
local version
if [ -n "${USE_CONDA}" ]; then
version="$(pyenv-exec conda --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (conda ${version:-unknown})"
else
if [ -n "$USE_M_VENV" ]; then
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (python -m venv)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (${M_VENV_PYTHON_BIN:-python} -m venv)"
else
version="$(pyenv-exec virtualenv --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
Expand All @@ -123,7 +127,7 @@ usage() {
pyenv-exec conda create --help 2>/dev/null || true
else
if [ -n "${USE_M_VENV}" ]; then
pyenv-exec python -m venv --help 2>/dev/null || true
pyenv-exec "${M_VENV_PYTHON_BIN:-python}" -m venv --help 2>/dev/null || true
else
pyenv-exec virtualenv --help 2>/dev/null || true
fi
Expand All @@ -141,9 +145,15 @@ detect_venv() {
if [ -x "${prefix}/bin/virtualenv" ]; then
HAS_VIRTUALENV=1
fi
if pyenv-exec python -m venv --help 1>/dev/null 2>&1; then
HAS_M_VENV=1
fi
# Prefer `python3.x` executable if avaialble (#206, #282)
local python
for python in "python${PYENV_VERSION%.*}" "python${PYENV_VERSION%%.*}" "python"; do
if pyenv-exec "${python}" -m venv --help 1>/dev/null 2>&1; then
HAS_M_VENV=1
M_VENV_PYTHON_BIN="${python}"
break
fi
done
fi
# Use `python -m venv` only if there is venv available, virtualenv is not installed, and `-p` not given
if [ -n "${HAS_CONDA}" ]; then
Expand Down Expand Up @@ -553,7 +563,7 @@ if [ -n "${USE_CONDA}" ]; then
pyenv-exec conda create $QUIET $VERBOSE --name "${VIRTUALENV_PATH##*/}" --yes "${VIRTUALENV_OPTIONS[@]}" python || STATUS="$?"
else
if [ -n "${USE_M_VENV}" ]; then
pyenv-exec python -m venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
pyenv-exec "${M_VENV_PYTHON_BIN:-python}" -m venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
else
pyenv-exec virtualenv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
fi
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 "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 "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 -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 python -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python3.5 -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 "python -m venv --help : true"
stub pyenv-exec "python3.5 -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
12 changes: 6 additions & 6 deletions test/pip.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ unstub_pyenv() {
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
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 "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 -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 python -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python3.5 -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1/envs/venv python -s -m ensurepip
rehashed
OUT
Expand All @@ -52,8 +52,8 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
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 "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 -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 +62,7 @@ OUT

assert_success
assert_output <<OUT
PYENV_VERSION=3.3.6 python -m venv ${PYENV_ROOT}/versions/3.3.6/envs/venv
PYENV_VERSION=3.3.6 python3.3 -m venv ${PYENV_ROOT}/versions/3.3.6/envs/venv
Installing pip from https://bootstrap.pypa.io/3.3/get-pip.py...
PYENV_VERSION=3.3.6/envs/venv python -s ${TMP}/pyenv/cache/get-pip.py
rehashed
Expand Down
4 changes: 4 additions & 0 deletions test/python.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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"
Expand All @@ -56,6 +58,8 @@ 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
16 changes: 10 additions & 6 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 "python -m venv --help : true"
stub pyenv-exec "python -m venv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
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 -s -m ensurepip : true"

run pyenv-virtualenv venv

assert_output <<OUT
PYENV_VERSION=3.5.1 python -m venv ${PYENV_ROOT}/versions/3.5.1/envs/venv
PYENV_VERSION=3.5.1 python3.5 -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 "python -m venv --help : true"
stub pyenv-exec "python3.5 -m venv --help : true"
stub pyenv-exec "virtualenv * : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : true"

Expand All @@ -77,6 +77,8 @@ 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 @@ -103,7 +105,7 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python -m venv --help : true"
stub pyenv-exec "python3.5 -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 @@ -130,7 +132,7 @@ OUT
stub_pyenv "${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
stub pyenv-virtualenv-prefix " : false"
stub pyenv-exec "python -m venv --help : true"
stub pyenv-exec "python3.5 -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 @@ -157,6 +159,8 @@ 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
8 changes: 6 additions & 2 deletions test/version.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ setup() {
@test "display virtualenv version" {
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 @@ -24,13 +27,14 @@ setup() {

@test "display venv version" {
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 "python -m venv --help : true"
stub pyenv-exec "python3.4 -m venv --help : true"

run pyenv-virtualenv --version

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

unstub pyenv-prefix
teardown_m_venv "3.4.1"
Expand Down
10 changes: 10 additions & 0 deletions test/virtualenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ 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} \"\$@\""
stub pyenv-exec "python -s -m ensurepip : false"
Expand Down Expand Up @@ -53,6 +55,8 @@ 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 @@ -81,6 +85,8 @@ 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 @@ -109,6 +115,8 @@ 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 @@ -172,6 +180,8 @@ 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

0 comments on commit 2d947be

Please sign in to comment.