-
-
Notifications
You must be signed in to change notification settings - Fork 258
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
A way to configure sys.executable flags at PEX runtime #2422
Comments
@bkad can you give a concrete example of what you're aiming for here? Passing flags to entry points is definitely supported already using |
This is what I'm trying to do, pass |
Ok. Yeah, you generally can't do this today. Only one incantation gets this to work, and then only after the 1st run:
Nets:
Why this works is fairly arcane, but basically In summary, @bkad this seems like a good feature to support across all PEX styles. |
For a bit more insight on the :; head -123 test.pex
#!/bin/sh
# N.B.: This script should stick to syntax defined for POSIX `sh` and avoid non-builtins.
# See: https://pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
set -eu
VENV=""
VENV_PYTHON_ARGS="-sE"
# N.B.: This ensures tilde-expansion of the DEFAULT_PEX_ROOT value.
DEFAULT_PEX_ROOT="$(echo ~/.pex)"
DEFAULT_PYTHON="/usr/bin/env"
DEFAULT_PYTHON_ARGS="python -u"
PEX_ROOT="${PEX_ROOT:-${DEFAULT_PEX_ROOT}}"
INSTALLED_PEX="${PEX_ROOT}/unzipped_pexes/466b0553ddeea983208b65ba0b5f9870e10b1127"
if [ -n "${VENV}" -a -x "${INSTALLED_PEX}" ]; then
# We're a --venv execution mode PEX installed under the PEX_ROOT and the venv
# interpreter to use is embedded in the shebang of our venv pex script; so just
# execute that script directly.
export PEX="$0"
if [ -n "${VENV_PYTHON_ARGS}" ]; then
exec "${INSTALLED_PEX}/bin/python" "${VENV_PYTHON_ARGS}" "${INSTALLED_PEX}" \
"$@"
else
exec "${INSTALLED_PEX}/bin/python" "${INSTALLED_PEX}" "$@"
fi
fi
find_python() {
for python in \
"python3.12" \
"python3.11" \
"python3.10" \
"python3.9" \
"python3.8" \
"python3.7" \
"python3.6" \
"python3.5" \
"python2.7" \
"pypy3.12" \
"pypy3.11" \
"pypy3.10" \
"pypy3.9" \
"pypy3.8" \
"pypy3.7" \
"pypy3.6" \
"pypy3.5" \
"pypy2.7" \
"python3" \
"python2" \
"pypy3" \
"pypy2" \
"python" \
"pypy" \
; do
if command -v "${python}" 2>/dev/null; then
return
fi
done
}
if [ -x "${DEFAULT_PYTHON}" ]; then
python_exe="${DEFAULT_PYTHON} ${DEFAULT_PYTHON_ARGS}"
else
python_exe="$(find_python)"
fi
if [ -n "${python_exe}" ]; then
if [ -n "${PEX_VERBOSE:-}" ]; then
echo >&2 "$0 used /bin/sh boot to select python: ${python_exe} for re-exec..."
fi
if [ -z "${VENV}" -a -e "${INSTALLED_PEX}" ]; then
# We're a --zipapp execution mode PEX installed under the PEX_ROOT with a
# __main__.py in our top-level directory; so execute Python against that
# directory.
export __PEX_EXE__="$0"
exec ${python_exe} "${INSTALLED_PEX}" "$@"
else
# The slow path: this PEX zipapp is not installed yet. Run the PEX zipapp so it
# can install itself, rebuilding its fast path layout under the PEX_ROOT.
if [ -n "${PEX_VERBOSE:-}" ]; then
echo >&2 "Running zipapp pex to lay itself out under PEX_ROOT."
fi
exec ${python_exe} "$0" "$@"
fi
fi
echo >&2 "Failed to find any of these python binaries on the PATH:"
for python in \
"python3.12" \
"python3.11" \
"python3.10" \
"python3.9" \
"python3.8" \
"python3.7" \
"python3.6" \
"python3.5" \
"python2.7" \
"pypy3.12" \
"pypy3.11" \
"pypy3.10" \
"pypy3.9" \
"pypy3.8" \
"pypy3.7" \
"pypy3.6" \
"pypy3.5" \
"pypy2.7" \
"python3" \
"python2" \
"pypy3" \
"pypy2" \
"python" \
"pypy" \
; do
echo >&2 "${python}"
done
echo >&2 "Either adjust your $PATH which is currently:"
echo >&2 "${PATH}"
echo >&2 -n "Or else install an appropriate Python that provides one of the binaries in "
echo >&2 "this list."
exit 1
P!
.bootstrap/P!.bootstrap/pex/P!x�_ek␦.bootstrap/pex/__init__.pySVp�/�,�L�(Q0204H�P((��JM.QH��+)�L*-�/*��RV��LN�+NMQ(�KI-R(�HUp,HLRP��Ԣ���<#=��T�����_���P!(�e��␦.bootstrap/pex/__main__.py5�� Basically all the Pex python bootstrap logic is replaced with shell to drop boot overhead from ~50ms to ~1ms (Python is SLOW). |
Noting that the trick to all this will be protecting |
Ok, some ctypes hackery to implement Footnotes
|
Support preserving and injecting Python args. Pex now supports `--inject-python-args` similar to `--inject-args` but for specifying arguments to pass to the Python interpreter as opposed to arguments to pass to the application code. This is supported for both zipapp and venv execution modes as well as standard shebang launching, launching via a Python explicitly or via the `--sh-boot` mechanism. In addition, PEX files now support detecting and passing through Python args embedded in shebangs or passed explicitly on the command line for all Pythons Pex supports save for `PyPy<3.10` where there appears to be no facility to retrieve the original argv PyPy was executed with. Closes #2422
Alrighty, this is now released on 2.4.0,: https://github.com/pex-tool/pex/releases/tag/v2.4.0 Thanks @bkad. |
Much appreciated! |
I noticed that Python flags are supported in interpreter mode using the
inject_args
flag and wonder if something similar can be supported running entry points.The text was updated successfully, but these errors were encountered: