sitecustomize.py not imported in a virtual environment
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
python3.10 (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
This is on Ubuntu 22.04, python3==3.10.4-3.
Run this:
$ python3.10 -mvenv ./venv
$ echo 'print("In sitecustomize.py")' > ./venv/
$ ./venv/bin/python -c ''
[nothing]
Expected output:
In sitecustomize.py
The reason this doesn't work is that the python3 package contains its own sitecustomize module (at /usr/lib/
I think the best way to fix this would be to:
1. Remove /usr/lib/
2. Add /usr/lib/
import apport_python_hook; apport_
This would have the same effect as the current sitecustomize.py, but would not conflict with user-provided sitecustomize modules.
Alternatively, /usr/lib/
This is still a major problem with Ubuntu 24.04 and python 3.12. I realize this choice has been in ubuntu for years after looking at other bug reports that were rejected. Sadly, this choice breaks projects that use the standard sitecustomize machinery to initialize their python environments and force them into really bad workarounds and hacks.
For anyone else stumbling into this, one workaround is to modify the /etc/python3. 12/sitecustomiz e.py file to have a block like this appended to the file:
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ## machinery, os, sys, types
# support a virtualenv's sitecustomize file
import importlib.
initial_paths = sys.path[:] sys.exec_ prefix) and p.endswith( "site-packages" ):
venv_sitecusto mize = os.path.join(p, "sitecustomize.py") exists( venv_sitecustom ize): machinery. SourceFileLoade r("sitecustomiz e", venv_sitecustomize)
sfl. exec_module( types.ModuleTyp e(sfl.name) ) ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ###
for p in initial_paths:
if p.startswith(
if os.path.
sfl = importlib.
#######
The suggestions in the descriptions are excellent and would allow the apport stuff to work and not impact serious python backend projects that never want to install or use the apport bug reporting features and need to be able to use sitecustomize in their virtual environments.