This commit is contained in:
Michał 2026-09-09 10:18:02 +00:00 committed by GitHub
commit 2287a9df8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 1 deletions

View file

@ -2,7 +2,14 @@
#
# Summary: Run an executable with the selected Python version
#
# Usage: pyenv exec <command> [arg1 arg2...]
# Usage: pyenv exec [-N|--environment] <command> [arg1 arg2...]
#
# -N Set PYTHONHOME and LD_LIBRARY_PATH envvars for the running command.
# This allows to run programs that embed Python without using rpath or
# exact library path.
# WARNING! For the running command and its child processes, this will
# break linkage for programs that expect to be linked to system libpython!
# This option is Linux only.
#
# Runs an executable by first preparing PATH so that the selected Python
# version's `bin' directory is at the front.
@ -18,9 +25,16 @@ set -e
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --environment
exec pyenv-shims --short
fi
unset MODIFY_ENV
if [ "$1" = "-N" ] || [ "$1" = "--environment" ]; then
MODIFY_ENV=true
shift
fi
PYENV_VERSION="$(pyenv-version-name -f)"
PYENV_COMMAND="$1"
@ -54,4 +68,24 @@ if [ "${PYENV_BIN_PATH#${PYENV_ROOT}}" != "${PYENV_BIN_PATH}" ]; then
# Only add to $PATH for non-system version.
export PATH="${PYENV_BIN_PATH}:${PATH}"
fi
if [ -n "$MODIFY_ENV" ]; then
if [ "$(uname -s)" != Linux ]; then
echo 'Error: the --environment option is supported only on Linux' >&2
exit 1
fi
# Check if multiple Python versions are selected
VERSIONS=$(pyenv-prefix)
if [[ "$VERSIONS" == *:* ]]; then
echo 'Warning: multiple Python versions are selected' >&2
VERSION="${VERSIONS%%:*}"
echo "Using the first one available (${VERSION##*/})" >&2
else
VERSION="$VERSIONS"
fi
# Assign the correct version of PYTHONHOME
export PYTHONHOME="$VERSION"
export LD_LIBRARY_PATH="${PYTHONHOME}/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
fi
exec "$PYENV_COMMAND_PATH" "$@"

View file

@ -34,6 +34,7 @@ EOF
assert_success
assert_output <<OUT
--help
--environment
fab
python
OUT
@ -139,3 +140,21 @@ $envvarname=/unusual/shim/location:/another/shim/location
_PYENV_SHIM_PATH=
!
}
@test "fails on non-Linux system" {
# Make sure to use the system version of Python
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
echo '' > .python-version
run uname -s
if [ "$output" != Linux ]; then
run pyenv-exec -N env
assert_failure
assert_output 'Error: the --environment option is supported only on Linux'
else
run pyenv-exec -N env
echo "$status"
assert_success
fi
}