summaryrefslogtreecommitdiff
path: root/gnu/packages/python.scm
diff options
context:
space:
mode:
authorMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-01-21 23:26:01 -0500
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2021-02-01 11:53:42 -0500
commitcb72f9a773e0931ee3758c851d96007ded034e4c (patch)
treece4a6c2c091203c604e7bfd395155731536e38d8 /gnu/packages/python.scm
parent6a4b336c4268e1f03517f4122d36640fa962db0d (diff)
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.
Diffstat (limited to 'gnu/packages/python.scm')
-rw-r--r--gnu/packages/python.scm57
1 files changed, 41 insertions, 16 deletions
diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index d3df1a4dabe..258e0749f04 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -96,13 +96,41 @@
96 #:use-module (gnu packages tcl) 96 #:use-module (gnu packages tcl)
97 #:use-module (gnu packages tls) 97 #:use-module (gnu packages tls)
98 #:use-module (gnu packages xml) 98 #:use-module (gnu packages xml)
99 #:use-module (guix gexp)
99 #:use-module (guix packages) 100 #:use-module (guix packages)
100 #:use-module (guix download) 101 #:use-module (guix download)
101 #:use-module (guix utils) 102 #:use-module (guix utils)
102 #:use-module (guix build-system gnu) 103 #:use-module (guix build-system gnu)
103 #:use-module (guix build-system trivial) 104 #:use-module (guix build-system trivial)
104 #:use-module (srfi srfi-1) 105 #:use-module (srfi srfi-1)
105 #:use-module (srfi srfi-26)) 106 #:use-module (srfi srfi-26)
107
108 #:export (customize-site
109 guix-pythonpath-search-path))
110
111(define* (customize-site version)
112 "Generate a install-sitecustomize.py phase, using VERSION."
113 `(lambda* (#:key inputs outputs #:allow-other-keys)
114 (let* ((out (assoc-ref outputs "out"))
115 (site-packages (string-append
116 out "/lib/python"
117 ,(version-major+minor version)
118 "/site-packages"))
119 (sitecustomize.py (assoc-ref inputs "sitecustomize.py"))
120 (dest (string-append site-packages "/sitecustomize.py")))
121 (mkdir-p site-packages)
122 (copy-file sitecustomize.py dest)
123 ;; Set the correct permissions on the installed file, else the byte
124 ;; compilation phase fails with a permission denied error.
125 (chmod dest #o644))))
126
127(define (guix-pythonpath-search-path version)
128 "Generate a GUIX_PYTHONPATH search path specification, using VERSION."
129 (search-path-specification (variable "GUIX_PYTHONPATH")
130 (files (list (string-append
131 "lib/python"
132 (version-major+minor version)
133 "/site-packages")))))
106 134
107(define-public python-2.7 135(define-public python-2.7
108 (package 136 (package
@@ -266,8 +294,7 @@
266 (not 294 (not
267 (string-prefix? "test_support." 295 (string-prefix? "test_support."
268 file)))))) 296 file))))))
269 (call-with-output-file "__init__.py" (const #t)) 297 (call-with-output-file "__init__.py" (const #t)))))))))
270 #t)))))))
271 (add-after 'remove-tests 'rebuild-bytecode 298 (add-after 'remove-tests 'rebuild-bytecode
272 (lambda* (#:key outputs #:allow-other-keys) 299 (lambda* (#:key outputs #:allow-other-keys)
273 (let ((out (assoc-ref outputs "out"))) 300 (let ((out (assoc-ref outputs "out")))
@@ -313,7 +340,9 @@
313 "/site-packages"))) 340 "/site-packages")))
314 (install-file tkinter.so target) 341 (install-file tkinter.so target)
315 (delete-file tkinter.so))))) 342 (delete-file tkinter.so)))))
316 #t)))))) 343 #t)))
344 (add-after 'install 'install-sitecustomize.py
345 ,(customize-site version)))))
317 (inputs 346 (inputs
318 `(("bzip2" ,bzip2) 347 `(("bzip2" ,bzip2)
319 ("expat" ,expat) 348 ("expat" ,expat)
@@ -327,15 +356,15 @@
327 ("tk" ,tk))) ; for tkinter 356 ("tk" ,tk))) ; for tkinter
328 (native-inputs 357 (native-inputs
329 `(("pkg-config" ,pkg-config) 358 `(("pkg-config" ,pkg-config)
359 ("sitecustomize.py" ,(local-file (search-auxiliary-file
360 "python/sitecustomize.py")))
330 ;; When cross-compiling, a native version of Python itself is needed. 361 ;; When cross-compiling, a native version of Python itself is needed.
331 ,@(if (%current-target-system) 362 ,@(if (%current-target-system)
332 `(("python2" ,this-package) 363 `(("python2" ,this-package)
333 ("which" ,which)) 364 ("which" ,which))
334 '()))) 365 '())))
335 (native-search-paths 366 (native-search-paths
336 (list (search-path-specification 367 (list (guix-pythonpath-search-path version)))
337 (variable "PYTHONPATH")
338 (files '("lib/python2.7/site-packages")))))
339 (home-page "https://www.python.org") 368 (home-page "https://www.python.org")
340 (synopsis "High-level, dynamically-typed programming language") 369 (synopsis "High-level, dynamically-typed programming language")
341 (description 370 (description
@@ -438,8 +467,7 @@ data types.")
438 (setenv "TZDIR" 467 (setenv "TZDIR"
439 (string-append (assoc-ref 468 (string-append (assoc-ref
440 (or native-inputs inputs) "tzdata") 469 (or native-inputs inputs) "tzdata")
441 "/share/zoneinfo")) 470 "/share/zoneinfo"))))
442 #t))
443 (replace 'rebuild-bytecode 471 (replace 'rebuild-bytecode
444 (lambda* (#:key outputs #:allow-other-keys) 472 (lambda* (#:key outputs #:allow-other-keys)
445 (let ((out (assoc-ref outputs "out"))) 473 (let ((out (assoc-ref outputs "out")))
@@ -462,8 +490,9 @@ data types.")
462 ;; Don't build lib2to3, because it's Python 2 code. 490 ;; Don't build lib2to3, because it's Python 2 code.
463 "-x" "lib2to3/.*" 491 "-x" "lib2to3/.*"
464 ,out)))) 492 ,out))))
465 (list "none" "-O" "-OO")) 493 (list "none" "-O" "-OO")))))
466 #t))))))) 494 (replace 'install-sitecustomize.py
495 ,(customize-site version))))))
467 (native-inputs 496 (native-inputs
468 `(("tzdata" ,tzdata-for-tests) 497 `(("tzdata" ,tzdata-for-tests)
469 ,@(if (%current-target-system) 498 ,@(if (%current-target-system)
@@ -471,11 +500,7 @@ data types.")
471 '()) 500 '())
472 ,@(package-native-inputs python-2))) 501 ,@(package-native-inputs python-2)))
473 (native-search-paths 502 (native-search-paths
474 (list (search-path-specification 503 (list (guix-pythonpath-search-path version)
475 (variable "PYTHONPATH")
476 (files (list (string-append "lib/python"
477 (version-major+minor version)
478 "/site-packages"))))
479 ;; Used to locate tzdata by the zoneinfo module introduced in 504 ;; Used to locate tzdata by the zoneinfo module introduced in
480 ;; Python 3.9. 505 ;; Python 3.9.
481 (search-path-specification 506 (search-path-specification