Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-128779: Fix site venv() for system site-packages#129184

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
FFY00 merged 2 commits intopython:mainfromvstinner:system_site
Jan 30, 2025

Conversation

@vstinner
Copy link
Member

@vstinnervstinner commentedJan 22, 2025
edited by bedevere-appbot
Loading

Add sys.base_exec_prefix to prefixes if pyvenv.cfg enables system site-packages.

Add sys.base_exec_prefix to prefixes if pyvenv.cfg enables systemsite-packages.
@vstinner
Copy link
MemberAuthor

cc:@FFY00

@befeleme
Copy link
Contributor

I can confirm this seems to fix the reported issue.

vstinner reacted with hooray emoji

Comment on lines 638 to 644
ifsystem_site=="true":
PREFIXES.insert(0,sys.prefix)
ifsys.base_exec_prefix!=sys.exec_prefix:
PREFIXES.append(sys.base_exec_prefix)
else:
PREFIXES= [sys.prefix]
ENABLE_USER_SITE=False
Copy link
Member

Choose a reason for hiding this comment

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

Sincesys.prefix is now set to point to the virtual environment before this code runs, addingsys.prefix here is now incorrect, we want to addsys.base_prefix andsys.base_exec_prefix, and because the existing directories inPREFIXES are now already the venv, we want to append the base prefixes instead.

Additionally, resettingPREFIXES whensystem_site isfalse is now unnecessary, because it already has the correct value.

Suggested change
ifsystem_site=="true":
PREFIXES.insert(0,sys.prefix)
ifsys.base_exec_prefix!=sys.exec_prefix:
PREFIXES.append(sys.base_exec_prefix)
else:
PREFIXES= [sys.prefix]
ENABLE_USER_SITE=False
ifsystem_site=="true":
PREFIXES+= [sys.base_prefix,sys.base_exec_prefix]
else:
ENABLE_USER_SITE=False

The comment above can also be removed, or moved togetsitepackages, as there's no possibility of us adding a duplicate prefix now.PREFIXES will already have a duplicated prefix from its initialization if we are running on a virtual environment.


Background into this:

GH-126987 moved the venv initialization logic togetpath, the code that initializes the prefixes andsys.path. Previously, that was done here insite, which was somewhat problematic, but you can read about that in the isssue.

So, before that change, whensite, initializedPREFIXES on import, it would effectively be set tosys.base_prefix andsys.base_exec_prefix, which at that point would always have the same value assys.prefix andsys.exec_prefix.1

PREFIXES= [sys.prefix,sys.exec_prefix]

Then, here invenv(), if we detect a virtual environment, we would setsys.prefix andsys.exec_prefix tosite_prefix in the snippet above that now issues warnings if we detect a mismatch (link to the change in GH-126987).

- sys.prefix = sys.exec_prefix = site_prefix+ if sys.prefix != site_prefix:+     _warn(f'Unexpected value in sys.prefix, expected {site_prefix}, got {sys.prefix}', RuntimeWarning)+ if sys.exec_prefix != site_prefix:+     _warn(f'Unexpected value in sys.exec_prefix, expected {site_prefix}, got {sys.exec_prefix}', RuntimeWarning)

After that, we either resetPREFIXES to[sys.prefix], ifsystem_site isfalse, or appendsys.prefix, which had now been updated tosite_prefix. We only do this forsys.prefix, and ignoresys.exec_prefix, because in virtual environments there's no distinction, they always have the same value.

Considering thegetpath changes, withPREFIXES getting set to[sys.prefix, sys.exec_prefix] on import, it already includes the virtual environment prefix, so all we need to do here now is to appendsys.base_prefix andsys.base_exec_prefix whensystem_site istrue.

Footnotes

  1. Why it was not explicitly initialized to the base prefixes, which should be the intent, I am not sure, but I guess that's an implementation detail that did not really matter at the time.

@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

@FFY00
Copy link
Member

I'm writing this here as a reminder for me later.

  • It would be nice to add a test forsys.path, including its order, intest_site
  • We should update thesite docstring to reflect the new changes

@vstinner
Copy link
MemberAuthor

@FFY00: I applied your suggestion, please review the updated PR.

@vstinner
Copy link
MemberAuthor

It would be nice to add a test for sys.path, including its order, in test_site

I tried but failed to write such test so far :-(

Copy link
Member

@FFY00FFY00 left a comment

Choose a reason for hiding this comment

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

Thanks, Victor!

I'll have a look to see if I can write a test for this.

vstinner reacted with thumbs up emojivstinner reacted with hooray emoji
@FFY00FFY00 merged commita549f43 intopython:mainJan 30, 2025
40 of 41 checks passed
@FFY00
Copy link
Member

I added a test inGH-129462.

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull requestFeb 7, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@FFY00FFY00FFY00 approved these changes

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@vstinner@befeleme@FFY00

[8]ページ先頭

©2009-2025 Movatter.jp