1
Fork 0
mirror of https://https.git.savannah.gnu.org/git/guix.git/ synced 2025-07-15 11:30:44 +02:00

gnu: python: Replace PYTHONPATH by GUIX_PYTHONPATH.

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 environment variable is read
from the environment, filtered for the current Python version of the
interpreter, and spliced in 'sys.path' just before Python's own site location,
which provides the expected behavior.

* gnu/packages/aux-files/python/sitecustomize.py: New file.
* Makefile.am: Register it.
* gnu/packages/python.scm (customize-site)
(guix-pythonpath-search-path): New procedures.
(python-2.7)[phases]{install-sitecustomize.py}: New phase.
[native-inputs]{sitecustomize.py}: New input.
[native-search-paths]: Replace PYTHONPATH with GUIX_PYTHONPATH.
(python-3.9)[native-search-paths]: Likewise.
[phases]{install-sitecustomize}: Override with correct version.
[native-search-paths]: Replace PYTHONPATH with GUIX_PYTHONPATH.
* gnu/packages/commencement.scm (python-boot0):
[phases]{install-sitecustomize}: Likewise.
[native-inputs]{sitecustomize.py}: New input.
[native-search-paths]: Replace PYTHONPATH with GUIX_PYTHONPATH.
* guix/build/python-build-system.scm (site-packages): Do not add a trailing
'/'.

squash! gnu: python: Replace PYTHONPATH by GUIX_PYTHONPATH.
This commit is contained in:
Maxim Cournoyer 2021-01-21 23:26:01 -05:00
parent 6a4b336c42
commit cb72f9a773
No known key found for this signature in database
GPG key ID: 1260E46482E63562
5 changed files with 95 additions and 22 deletions

View file

@ -96,13 +96,41 @@
#:use-module (gnu packages tcl)
#:use-module (gnu packages tls)
#:use-module (gnu packages xml)
#:use-module (guix gexp)
#:use-module (guix packages)
#:use-module (guix download)
#:use-module (guix utils)
#:use-module (guix build-system gnu)
#:use-module (guix build-system trivial)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26))
#:use-module (srfi srfi-26)
#:export (customize-site
guix-pythonpath-search-path))
(define* (customize-site version)
"Generate a install-sitecustomize.py phase, using VERSION."
`(lambda* (#:key inputs outputs #:allow-other-keys)
(let* ((out (assoc-ref outputs "out"))
(site-packages (string-append
out "/lib/python"
,(version-major+minor version)
"/site-packages"))
(sitecustomize.py (assoc-ref inputs "sitecustomize.py"))
(dest (string-append site-packages "/sitecustomize.py")))
(mkdir-p site-packages)
(copy-file sitecustomize.py dest)
;; Set the correct permissions on the installed file, else the byte
;; compilation phase fails with a permission denied error.
(chmod dest #o644))))
(define (guix-pythonpath-search-path version)
"Generate a GUIX_PYTHONPATH search path specification, using VERSION."
(search-path-specification (variable "GUIX_PYTHONPATH")
(files (list (string-append
"lib/python"
(version-major+minor version)
"/site-packages")))))
(define-public python-2.7
(package
@ -266,8 +294,7 @@
(not
(string-prefix? "test_support."
file))))))
(call-with-output-file "__init__.py" (const #t))
#t)))))))
(call-with-output-file "__init__.py" (const #t)))))))))
(add-after 'remove-tests 'rebuild-bytecode
(lambda* (#:key outputs #:allow-other-keys)
(let ((out (assoc-ref outputs "out")))
@ -313,7 +340,9 @@
"/site-packages")))
(install-file tkinter.so target)
(delete-file tkinter.so)))))
#t))))))
#t)))
(add-after 'install 'install-sitecustomize.py
,(customize-site version)))))
(inputs
`(("bzip2" ,bzip2)
("expat" ,expat)
@ -327,15 +356,15 @@
("tk" ,tk))) ; for tkinter
(native-inputs
`(("pkg-config" ,pkg-config)
("sitecustomize.py" ,(local-file (search-auxiliary-file
"python/sitecustomize.py")))
;; When cross-compiling, a native version of Python itself is needed.
,@(if (%current-target-system)
`(("python2" ,this-package)
("which" ,which))
'())))
(native-search-paths
(list (search-path-specification
(variable "PYTHONPATH")
(files '("lib/python2.7/site-packages")))))
(list (guix-pythonpath-search-path version)))
(home-page "https://www.python.org")
(synopsis "High-level, dynamically-typed programming language")
(description
@ -438,8 +467,7 @@ data types.")
(setenv "TZDIR"
(string-append (assoc-ref
(or native-inputs inputs) "tzdata")
"/share/zoneinfo"))
#t))
"/share/zoneinfo"))))
(replace 'rebuild-bytecode
(lambda* (#:key outputs #:allow-other-keys)
(let ((out (assoc-ref outputs "out")))
@ -462,8 +490,9 @@ data types.")
;; Don't build lib2to3, because it's Python 2 code.
"-x" "lib2to3/.*"
,out))))
(list "none" "-O" "-OO"))
#t)))))))
(list "none" "-O" "-OO")))))
(replace 'install-sitecustomize.py
,(customize-site version))))))
(native-inputs
`(("tzdata" ,tzdata-for-tests)
,@(if (%current-target-system)
@ -471,11 +500,7 @@ data types.")
'())
,@(package-native-inputs python-2)))
(native-search-paths
(list (search-path-specification
(variable "PYTHONPATH")
(files (list (string-append "lib/python"
(version-major+minor version)
"/site-packages"))))
(list (guix-pythonpath-search-path version)
;; Used to locate tzdata by the zoneinfo module introduced in
;; Python 3.9.
(search-path-specification