This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mppf
Recipients mppf, ned.deily, ronaldoussoren
Date 2020-11-10.15:20:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605021627.33.0.143648905308.issue42312@roundup.psfhosted.org>
In-reply-to
Content
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)
History
Date User Action Args
2020-11-10 15:20:27mppfsetrecipients: + mppf, ronaldoussoren, ned.deily
2020-11-10 15:20:27mppfsetmessageid: <1605021627.33.0.143648905308.issue42312@roundup.psfhosted.org>
2020-11-10 15:20:27mppflinkissue42312 messages
2020-11-10 15:20:27mppfcreate