New submission from Michael Ferguson <michael.fergu...@hpe.com>:
I have been trying to create a wrapper script for `python3` in a venv that behaves similarly to a symbolic link. I am able to use `exec -a` in bash to run `python3` with `argv[0]` set to the wrapper script. This allows it to function similarly to the symbolic link in a venv. However, this approach does not work on Mac OS X with a homebrew installation. I think this is a bug. Here are the simple steps to reproduce (assuming bash shell): ``` cd /tmp python3 -m venv test-venv (exec -a test-venv/python3 python3 -c 'import sys; print(sys.executable); print (sys.prefix);') ``` ### Good output (Ubuntu 20.04) /tmp/test-venv/python-wrapper /tmp ### Bad output (Homebrew on Mac OS X) /usr/local/opt/python@3.9/bin/python3.9 /usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9 Here are some things that might be related: * the Mac OS X framework launcher and how it uses `realpath` (and issue22490) * `site.py` code in `def venv` and the conditional on `__PYVENV_LAUNCHER__`. The `if` branch is not being run in this configuration. * setting the environment variable `PYTHONEXECUTABLE` (e.g. `export PYTHONEXECUTABLE=test-venv/python3` before the other commands) causes the `if` branch in the conditional on `__PYVENV_LAUNCHER__` in `site.py` `def venv` to be run. This allows `sys.executable` to be set as expected but `sys.prefix` is still wrong. If you are interested in something closer to the use case, the below explains how to get a more user-facing reproducer: $ python3 -m venv test-venv -- put this into test-venv/python-wrapper -- #!/usr/bin/env bash # remove path component to prevent infinite loop export PATH=${PATH//test-venv/missing} # Now run the real python3 interpreter but tell it that it # is being launched at the current path, so it can # correctly find dependencies in the venv exec -a "$0" python3 "$@" $ chmod a+x test-venv/python-wrapper $ ./test-venv/python-wrapper -c 'import sys; print(sys.executable); print (sys.prefix);' (and with this script the problematic behavior is exactly the same as the exec commands above) ---------- components: macOS messages: 380677 nosy: mppf, ned.deily, ronaldoussoren priority: normal severity: normal status: open title: sys.prefix is set incorrectly on Mac OS X type: behavior versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue42312> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com