Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Add sys.base_exec_prefix to prefixes if pyvenv.cfg enables systemsite-packages.
cc:@FFY00 |
I can confirm this seems to fix the reported issue. |
| 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 |
There was a problem hiding this comment.
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.
| 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
Line 80 ine635bf2
| 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
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.↩
When you're done making the requested changes, leave the comment: |
I'm writing this here as a reminder for me later.
|
@FFY00: I applied your suggestion, please review the updated PR. |
I tried but failed to write such test so far :-( |
There was a problem hiding this 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.
a549f43 intopython:mainUh oh!
There was an error while loading.Please reload this page.
I added a test inGH-129462. |
Uh oh!
There was an error while loading.Please reload this page.
Add sys.base_exec_prefix to prefixes if pyvenv.cfg enables system site-packages.
--system-site-packagesdoesn't work (system packages are not detected) #128779