From 762357609270ab016236d22999ae5cfc3fe4ff28 Mon Sep 17 00:00:00 2001 From: Maxim Cournoyer Date: Fri, 3 Dec 2021 22:36:26 -0500 Subject: [PATCH] sitecustomize.py: Honor .pth files. Fixes . * gnu/packages/aux-files/python/sitecustomize.py: Use site.addsitedirs to add the site directories; this takes care of the .pth files. Make sure the added items still appear before Python's own 'site-packages' directory. --- .../aux-files/python/sitecustomize.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/gnu/packages/aux-files/python/sitecustomize.py b/gnu/packages/aux-files/python/sitecustomize.py index 71e328b9ac..bdaaa8e9e2 100644 --- a/gnu/packages/aux-files/python/sitecustomize.py +++ b/gnu/packages/aux-files/python/sitecustomize.py @@ -18,6 +18,7 @@ # along with GNU Guix. If not, see . import os +import site import sys # Commentary: @@ -47,9 +48,20 @@ all_sites_norm = [os.path.normpath(p) for p in all_sites_raw] matching_sites = [p for p in all_sites_norm if p.endswith(site_packages_prefix)] -# Insert sites matching the current version into sys.path, right before -# Python's own site. This way, the user can override the libraries provided -# by Python itself. -sys_path_absolute = [os.path.realpath(p) for p in sys.path] -index = sys_path_absolute.index(python_site) -sys.path[index:index] = matching_sites +if not matching_sites: + exit(0) + +# Deduplicate the entries, append them to sys.path, and handle any .pth files +# they contain. +for s in matching_sites: + site.addsitedir(s) + +# Move the entries that were appended to sys.path in front of Python's own +# site-packages directory. This enables Guix packages to override Python's +# bundled packages, such as 'pip'. +python_site_index = sys.path.index(python_site) +new_site_start_index = sys.path.index(matching_sites[0]) +if python_site_index < new_site_start_index: + sys.path = (sys.path[:python_site_index] + + sys.path[new_site_start_index:] + + sys.path[python_site_index:new_site_start_index]) -- 2.34.0