Description
It seems there is a bug in venv (and a similar one in virtualenv) where the "base" path in pyvenv.cfg is set incorrectly. If Python is installed in a non-standard folder, e.g. /usr/local/python-X.Y.Z and then symlinked into /usr/local/bin/python3, the venv package does not work correctly. I believe the source of the trouble is the setting of "home" variable. Specifically, this line:
dirname, exename = os.path.split(os.path.abspath(executable))
The dirname
result is used to set "home". If the executable path is /usr/local/bin/python3 (actually a symlink to /usr/local/python-X.Y.Z/bin/python3), the "home" should not be set to /usr/local. Changing the above line (in the ensure_directories()
) function to:
dirname, exename = os.path.split(os.path.realpath(executable))
This fixes the problem. I believe this is consistent with what the getpath.py module in Python does.
I noticed with problem when running the most recent Debian OS, version 12. It includes Python 3.11 and therefore /usr/lib/python3.11 exists. With the above bug, the venv tries to use /usr/lib/python3.11 as the sys.path. Importing the struct
module fails with the mysterious error:
>>> import struct
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.11/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'