pyenv-exec: add option to modify python runtime for the called executable

The only variables modified are $PYTHONHOME and $LD_LIBRARY_PATH which will allow the executable to use the shared library of the Python version which is selected with pyenv.
This commit is contained in:
Romwierz 2026-09-07 18:10:43 +02:00
parent ef7a9f5d14
commit a4f80ce23f

View file

@ -2,7 +2,9 @@
#
# Summary: Run an executable with the selected Python version
#
# Usage: pyenv exec <command> [arg1 arg2...]
# Usage: pyenv exec [-E|--environment] <command> [arg1 arg2...]
#
# -E Modify Python runtime environment, such as $PYTHONHOME, for the called executable.
#
# Runs an executable by first preparing PATH so that the selected Python
# version's `bin' directory is at the front.
@ -18,9 +20,16 @@ set -e
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --environment
exec pyenv-shims --short
fi
unset MODIFY_ENV
if [ "$1" = "-E" ] || [ "$1" = "--environment" ]; then
MODIFY_ENV=true
shift
fi
PYENV_VERSION="$(pyenv-version-name -f)"
PYENV_COMMAND="$1"
@ -54,4 +63,8 @@ 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
export PYTHONHOME="$(pyenv-prefix)"
export LD_LIBRARY_PATH="${PYTHONHOME}/lib:${LD_LIBRARY_PATH}"
fi
exec "$PYENV_COMMAND_PATH" "$@"