GNU bug report logs - #46028
[PATCH core-updates 0/2] Farewell to PYTHONPATH.

Previous Next

Package: guix-patches;

Reported by: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>

Date: Fri, 22 Jan 2021 04:49:01 UTC

Severity: normal

Tags: patch

Done: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


Message #11 received at 46028 <at> debbugs.gnu.org (full text, mbox):

From: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
To: 46028 <at> debbugs.gnu.org
Cc: Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
Subject: [PATCH core-updates 2/2] gnu: python: Replace PYTHONPATH by
 GUIX_PYTHONPATH_X_Y.
Date: Thu, 21 Jan 2021 23:51:04 -0500
Using PYTHONPATH as a mean to discover the Python packages had the following
issues:

        1. It is not versioned, so different versions of Python would clash if
        installed in a shared profile.

        2. It would interfere with the host Python site on foreign
        distributions, sometimes preventing a a user to login their GDM
        session (!).

        3. It would take precedence over user installed Python packages
        installed through pip.

        4. It would leak into Python virtualenvs, which are supposed to create
        isolated Python environments.

This changes fixes the above issues by making use of a sitecustomize.py
module.  The newly introduced GUIX_PYTHONPATH_X_Y environment variable, where
X is the major and Y the minor version numbers, is read from the environment
and spliced in 'sys.path' just before Python's own site location, which
provides the expected behavior.

* gnu/packages/python.scm (python-2.7)[phases]{customize-site}: Add phase.
[native-search-paths]: Modify so that GUIX_PYTHONPATH_X_Y is used.
* gnu/packages/python.scm (python-3.9)[native-search-paths]: Likewise.
---
 gnu/packages/python.scm | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index d3df1a4dab..79cea3d43a 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -313,7 +313,30 @@
                                     "/site-packages")))
                       (install-file tkinter.so target)
                       (delete-file tkinter.so)))))
-               #t))))))
+               #t)))
+         (add-after 'install 'customize-site
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let* ((out (assoc-ref outputs "out"))
+                    (site-packages (string-append
+                                    out "/lib/python"
+                                    ,(version-major+minor version)
+                                    "/site-packages"))
+                    (pythonpath (format #f "GUIX_PYTHONPATH_~a_~a"
+                                        ,(version-major version)
+                                        ,(version-minor version)))
+                    (sitecustomize.py (string-append site-packages
+                                                     "/sitecustomize.py")))
+               (with-output-to-file sitecustomize.py
+                 (lambda _
+                   (format #t "\
+import os
+import sys
+
+# Insert the GUIX_PYTHONPATH_X_Y values just before Python's own site.
+sys_path_absolute = [os.path.realpath(p) for p in sys.path]
+index = sys_path_absolute.index(~s)
+sys.path = sys.path[:index] + os.environ[~s].split(':') + sys.path[index:]
+" site-packages pythonpath)))))))))
     (inputs
      `(("bzip2" ,bzip2)
        ("expat" ,expat)
@@ -334,8 +357,12 @@
              '())))
     (native-search-paths
      (list (search-path-specification
-            (variable "PYTHONPATH")
-            (files '("lib/python2.7/site-packages")))))
+            (variable (format #f "GUIX_PYTHONPATH_~a_~a"
+                              (version-major version)
+                              (version-minor version)))
+            (files (list (string-append "lib/python"
+                                        (version-major+minor version)
+                                        "/site-packages"))))))
     (home-page "https://www.python.org")
     (synopsis "High-level, dynamically-typed programming language")
     (description
@@ -472,7 +499,9 @@ data types.")
        ,@(package-native-inputs python-2)))
     (native-search-paths
      (list (search-path-specification
-            (variable "PYTHONPATH")
+            (variable (format #f "GUIX_PYTHONPATH_~a_~a"
+                              (version-major version)
+                              (version-minor version)))
             (files (list (string-append "lib/python"
                                         (version-major+minor version)
                                         "/site-packages"))))
-- 
2.29.2





This bug report was last modified 4 years and 171 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.