You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
site.py is no longer read at startup time (as it is a deep-frozen module) #107344
The change was introduced in order to optimise the startup time. It would be retrograde to stop doing that, but at the same time, having .py files in the shipped distribution (stdlib) which are not used is confusing and wasteful. At a minimum we could no longer ship frozen .py files (since they aren't used), thereby saving disk space and avoiding confusion for people who might edit those files.
If the startup time optimisation is a result of avoiding stat-ing files, then it would be great if there was a convenient way to re-compile the site.py frozen module (so that we can still customize the site). If it isn't such a concern to stat, the alternative would be to look to see if there is a site.py file, and if so, use that instead of the frozen one (assuming that the site.py is removed from the distribution as suggested above).
Above all, the desire is to be able to configure the site of a distribution after compilation, without using tricks such as sitecustomize, activations scripts, or .pth files.
Your environment
I have reproduced this on Python 3.11.4 and main (6e850c3), on Linux.
There is already third-party package code which has been executed by the time sitecustomize is run (via .pth), and I need to influence how all code is executed (e.g. setting default environment variables before anything else)
I want to be able to add additional site-packages directories, with higher precedence than user site-packages (an prior processing in e.g. .pth files should follow that precedence)
It would be nice if the consumers of my built Python distribution (which they can install themselves) could configure the distribution using the standard sitecustomize mechanism, but this isn't possible if I use sitecustomize for controlling the distribution's behaviour itself. Essentially, I am a Python distributor (i.e. I provide an installer), not a Python distribution provider, and at the same time, I am re-using an existing build, not building from source (so can't patch site).
To be clear, my biggest concern is that site.py is shipped (in the stdlib), but is entirely ignored. I would find it more appropriate to either not ship site.py at all, or to provide a mechanism that allows overriding a frozen module (though I acknowledge that this implies more failed file stats in most cases, which might invalidate some of the benefit of the change in the first place?)
I unfortunately come across this issue. massive outage when some service accounts got a ~/.local/lib/python3.11/site-packages/... populating by something.
the site.py was edited to set site.ENABLE_USER_SITE = False to prevent exactly this issue from happening.
but that stopped working as per this bug ticket :(
I went through the bug tracker and I haven't seen any mention of a fix or any workaround to address the issue.
The alternative I could come up is to add this snippet to the top of your sitecustomize.py, for lack of a better solution.
However it is not equivalent to the previous solution of editing site.py, because the sitecustomize.py is loaded after the import paths have been configured to load from the user site.
the sitecustomize.py cannot prevent from loading packages from the user sites, it's called too late.
import site
import sys
# ~/.local/lib/python3.8/site-packages
# disable user site otherwise it takes precedence over packages in the venv.
# it was meant to be disabled by editing the site.py file, but that stopped working.
# https://github.com/python/cpython/issues/107344
site.ENABLE_USER_SITE = False
user_site = site.getusersitepackages()
if user_site in sys.path:
sys.path.remove(user_site)
You can run the interpreter with python -v and see that imports are still contaminated by the .local directory.
The original change came from #28398. Perhaps @ericsnowcurrently could help us determine if there is something we can do to conditionally run site.py (not the frozen version) during startup only if it is changed?
Activity
njsmith commentedon Jul 28, 2023
Can you elaborate on why
sitecustomizedoesn't work for you? Is the issue that it's missing features, or something else?pelson commentedon Jul 28, 2023
Mostly
sitecustomizecan work, but a few reasons that it is more challenging/impossible:sitecustomize(your issue It should be possible for a Python environment to disable the "user site" functionality without patching the stdlib #99312)sitecustomizeis run (via.pth), and I need to influence how all code is executed (e.g. setting default environment variables before anything else)site-packagesdirectories, with higher precedence than user site-packages (an prior processing in e.g..pthfiles should follow that precedence)sitecustomizemechanism, but this isn't possible if I usesitecustomizefor controlling the distribution's behaviour itself. Essentially, I am a Python distributor (i.e. I provide an installer), not a Python distribution provider, and at the same time, I am re-using an existing build, not building from source (so can't patchsite).To be clear, my biggest concern is that
site.pyis shipped (in the stdlib), but is entirely ignored. I would find it more appropriate to either not shipsite.pyat all, or to provide a mechanism that allows overriding a frozen module (though I acknowledge that this implies more failed file stats in most cases, which might invalidate some of the benefit of the change in the first place?)morotti commentedon Jan 22, 2025
I unfortunately come across this issue. massive outage when some service accounts got a
~/.local/lib/python3.11/site-packages/...populating by something.the site.py was edited to set
site.ENABLE_USER_SITE = Falseto prevent exactly this issue from happening.but that stopped working as per this bug ticket :(
I went through the bug tracker and I haven't seen any mention of a fix or any workaround to address the issue.
The alternative I could come up is to add this snippet to the top of your
sitecustomize.py, for lack of a better solution.However it is not equivalent to the previous solution of editing
site.py, because thesitecustomize.pyis loaded after the import paths have been configured to load from the user site.the sitecustomize.py cannot prevent from loading packages from the user sites, it's called too late.
You can run the interpreter with
python -vand see that imports are still contaminated by the .local directory.pelson commentedon Jan 23, 2025
The original change came from #28398. Perhaps @ericsnowcurrently could help us determine if there is something we can do to conditionally run site.py (not the frozen version) during startup only if it is changed?
StanFromIreland commentedon May 11, 2026
@FFY00 FYI pinning this issue pins it for everyone, as such it shows up here:
I presume this wasn't what you intended, so I'll unpin it.
FFY00 commentedon May 19, 2026
Thanks! I am pretty sure I accidentally pinned it on mobile when trying to subscribe 😅