FindPython: Debian-specific 'posix_local' scheme interferes with Python_SITELIB and Python_SITEARCH
As a side effect of !8538 (merged), the Debian-specific sysconfig scheme posix_local
is now exposed to CMake, which is the default for the /usr
prefix and injects /local/
into Python_SITELIB
and Python_SITEARCH
. The scheme has effectively been in use by Debian's distutils for many years, but it was recently (2022) taught as proper scheme to the sysconfig
module, which fixes other breakage but also causes this bug.
In the long term, Debian may change their policy to better align with other platforms, but as a short term fix, we probably need to override the default scheme, i.e. pass scheme='posix_prefix'
to sysconfig.get_path
. Any of the following conditions would be sufficient to detect whether the scheme override has to happen:
- Check if
'posix_local' in sysconfig.get_scheme_names()
- Check if Python is version 3.10 or later and
sysconfig.get_default_scheme() == 'posix_local'
- Check if CMake runs on Debian or a Debian derivative and Python is version 3.10 or later
The most interoperable Python code I could come up with is:
try:
import sysconfig
get_default_scheme = getattr(sysconfig, 'get_default_scheme' if hasattr(sysconfig, 'get_default_scheme') else '_get_default_scheme')
scheme = get_default_scheme()
if scheme == 'posix_local':
scheme = 'posix_prefix'
sys.stdout.write(';'.join([sysconfig.get_path('stdlib', scheme), sysconfig.get_path('platstdlib', scheme), sysconfig.get_path('purelib', scheme), sysconfig.get_path('platlib', scheme)]))
except Exception:
from distutils import sysconfig
# ...