Skip to content

getpath: Add comments highlighing details of the pyvenv.cfg detection #127966

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

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,20 @@ def search_up(prefix, *landmarks, test=isfile):
venv_prefix = None
pyvenvcfg = []

# Search for the 'home' key in pyvenv.cfg. Currently, we don't consider the
# presence of a pyvenv.cfg file without a 'home' key to signify the
# existence of a virtual environment — we quietly ignore them.
# XXX: If we don't find a 'home' key, we don't look for another pyvenv.cfg!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a bit confusing - there is only one searched path for pyvenv.cfg. Are you suggesting that there should be multiple places being looked at for pyvenv.cfg?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay getting back to you.

No, there are two paths, {executable_dir} and {executable_dir}/... Using the venv install scheme, pyvenv.cfg will be at {executable_dir}/.., but implementation also allows it to be alongside the executable.

See https://docs.python.org/3.14/c-api/init_config.html#init-path-config

If home is not set and a pyvenv.cfg file is present in the same directory as executable, or its parent, (...)

for line in pyvenvcfg:
key, had_equ, value = line.partition('=')
if had_equ and key.strip().lower() == 'home':
# Override executable_dir/real_executable_dir with the value from 'home'.
# These values may be later used to calculate prefix/base_prefix, if a more
# reliable source — like the runtime library (libpython) path — isn't available.
executable_dir = real_executable_dir = value.strip()
# If base_executable — which points to the Python interpreted from
# the base installation — isn't set (eg. when embedded), try to find
# it in 'home'.
if not base_executable:
# First try to resolve symlinked executables, since that may be
# more accurate than assuming the executable in 'home'.
Expand Down Expand Up @@ -400,6 +410,7 @@ def search_up(prefix, *landmarks, test=isfile):
break
break
else:
# We didn't find a 'home' key in pyvenv.cfg (no break), reset venv_prefix.
venv_prefix = None


Expand Down
Loading