sys._base_executable resolves symlinks too eagerly, resulting in undesirable venv from venv behaviour #128670
Labels
stdlib
Python modules in the Lib dir
topic-installation
topic-venv
Related to the venv module
type-bug
An unexpected behavior, bug, or error
Bug report
Bug description:
In #106045, the use case of putting a symlink in
/usr/local/bin
to a Python binary from another prefix was highlighted. For homebrew, this use case is used with the idea that the symlink is the public interface/location, and the prefix where it is actually installed is an implementation detail (and can change over time) (more details in astral-sh/uv#1640).#106045 seems to have now been resolved by #127974 (I wrote a test for that). The originating issue proposed a solution which would fix the problem with the broken virtual environment in that case, but would actually break the aforementioned homebrew example by eagerly resolving the symlinked executable and using the resolved path to determine the
home
value inpyvenv.cfg
- thereby exposing the "internal" prefix intopyvenv.cfg
.The problem I am reporting here is that
sys._base_executable
eagerly resolves symlinks ingetpath.py
venv
,virtualenv
anduv
all usesys._base_executable
to determine thehome
locationhome
location will be resolved, whereas the first will not be. Thereby bleeding the implementation detail once more.In
uv
, the behaviour was solidified in astral-sh/uv#8433 to avoid exposing the internal prefix in the venv. Note thatvirtualenv
is (accidentally?) now exposing the internal prefix even on the first virtual environment pypa/virtualenv#2770.To reproduce this:
And observe that the second venv's home is not
/tmp/public/bin
but the "internal detail" one.A quick test for getpath, and a complete test for venv are included below (both failing):
cc @FFY00 following on from our conversation in #127974 (comment) (I would love to get a commit for the tests, if not the fix 😜).
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: