1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
3 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; Copyright © 2013, 2014, 2015, 2016 Andreas Enge <andreas@enge.fr>
5 ;;; Copyright © 2014, 2015 Mark H Weaver <mhw@netris.org>
6 ;;; Copyright © 2014, 2017, 2019 Eric Bavier <bavier@member.fsf.org>
7 ;;; Copyright © 2014, 2015 Federico Beffa <beffa@fbengineering.ch>
8 ;;; Copyright © 2015 Omar Radwan <toxemicsquire4@gmail.com>
9 ;;; Copyright © 2015 Pierre-Antoine Rault <par@rigelk.eu>
10 ;;; Copyright © 2015, 2016, 2017, 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
11 ;;; Copyright © 2015, 2016 Christopher Allan Webber <cwebber@dustycloud.org>
12 ;;; Copyright © 2015 Eric Dvorsak <eric@dvorsak.fr>
13 ;;; Copyright © 2015, 2016 David Thompson <davet@gnu.org>
14 ;;; Copyright © 2015, 2016, 2017 Leo Famulari <leo@famulari.name>
15 ;;; Copyright © 2015, 2017 Ben Woodcroft <donttrustben@gmail.com>
16 ;;; Copyright © 2015, 2016 Erik Edrosa <erik.edrosa@gmail.com>
17 ;;; Copyright © 2015, 2016, 2017, 2018, 2019 Efraim Flashner <efraim@flashner.co.il>
18 ;;; Copyright © 2015, 2017 Kyle Meyer <kyle@kyleam.com>
19 ;;; Copyright © 2015, 2016 Chris Marusich <cmmarusich@gmail.com>
20 ;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
21 ;;; Copyright © 2016 Lukas Gradl <lgradl@openmailbox.org>
22 ;;; Copyright © 2016, 2018 Hartmut Goebel <h.goebel@crazy-compilers.com>
23 ;;; Copyright © 2016 Daniel Pimentel <d4n1@d4n1.org>
24 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@gmail.com>
25 ;;; Copyright © 2016, 2017 Troy Sankey <sankeytms@gmail.com>
26 ;;; Copyright © 2016, 2017 ng0 <ng0@n0.is>
27 ;;; Copyright © 2016 Dylan Jeffers <sapientech@sapientech@openmailbox.org>
28 ;;; Copyright © 2016 David Craven <david@craven.ch>
29 ;;; Copyright © 2016, 2017, 2018, 2019 Marius Bakke <mbakke@fastmail.com>
30 ;;; Copyright © 2016, 2017 Stefan Reichör <stefan@xsteve.at>
31 ;;; Copyright © 2016 Dylan Jeffers <sapientech@sapientech@openmailbox.org>
32 ;;; Copyright © 2016, 2017 Alex Vong <alexvong1995@gmail.com>
33 ;;; Copyright © 2016, 2017, 2018 Arun Isaac <arunisaac@systemreboot.net>
34 ;;; Copyright © 2016, 2017, 2018 Julien Lepiller <julien@lepiller.eu>
35 ;;; Copyright © 2016, 2017, 2018 Tobias Geerinckx-Rice <me@tobias.gr>
36 ;;; Copyright © 2016, 2017 Thomas Danckaert <post@thomasdanckaert.be>
37 ;;; Copyright © 2017 Carlo Zancanaro <carlo@zancanaro.id.au>
38 ;;; Copyright © 2017 Frederick M. Muriithi <fredmanglis@gmail.com>
39 ;;; Copyright © 2017, 2018 Adriano Peluso <catonano@gmail.com>
40 ;;; Copyright © 2017 Ben Sturmfels <ben@sturm.com.au>
41 ;;; Copyright © 2017, 2018, 2019 Mathieu Othacehe <m.othacehe@gmail.com>
42 ;;; Copyright © 2017 José Miguel Sánchez García <jmi2k@openmailbox.org>
43 ;;; Copyright © 2017 Roel Janssen <roel@gnu.org>
44 ;;; Copyright © 2017, 2018 Kei Kebreau <kkebreau@posteo.net>
45 ;;; Copyright © 2017 Rutger Helling <rhelling@mykolab.com>
46 ;;; Copyright © 2017 Muriithi Frederick Muriuki <fredmanglis@gmail.com>
47 ;;; Copyright © 2017 Brendan Tildesley <mail@brendan.scot>
48 ;;; Copyright © 2018 Ethan R. Jones <ethanrjones97@gmail.com
49 ;;; Copyright © 2018 Fis Trivial <ybbs.daans@hotmail.com>
50 ;;; Copyright © 2018 Vijayalakshmi Vedantham <vijimay12@gmail.com>
51 ;;; Copyright © 2018 Mathieu Lirzin <mthl@gnu.org>
52 ;;; Copyright © 2018 Adam Massmann <massmannak@gmail.com>
53 ;;; Copyright © 2016, 2018 Tomáš Čech <sleep_walker@gnu.org>
54 ;;; Copyright © 2018 Nicolas Goaziou <mail@nicolasgoaziou.fr>
55 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
56 ;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
57 ;;; Copyright © 2018, 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
58 ;;; Copyright © 2018 Luther Thompson <lutheroto@gmail.com>
59 ;;; Copyright © 2018 Vagrant Cascadian <vagrant@debian.org>
61 ;;; This file is part of GNU Guix.
63 ;;; GNU Guix is free software; you can redistribute it and/or modify it
64 ;;; under the terms of the GNU General Public License as published by
65 ;;; the Free Software Foundation; either version 3 of the License, or (at
66 ;;; your option) any later version.
68 ;;; GNU Guix is distributed in the hope that it will be useful, but
69 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
70 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 ;;; GNU General Public License for more details.
73 ;;; You should have received a copy of the GNU General Public License
74 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
76 (define-module (gnu packages python)
77 #:use-module ((guix licenses) #:prefix license:)
78 #:use-module (gnu packages)
79 #:use-module (gnu packages base)
80 #:use-module (gnu packages bash)
81 #:use-module (gnu packages compression)
82 #:use-module (gnu packages dbm)
83 #:use-module (gnu packages libffi)
84 #:use-module (gnu packages pkg-config)
85 #:use-module (gnu packages readline)
86 #:use-module (gnu packages sqlite)
87 #:use-module (gnu packages tcl)
88 #:use-module (gnu packages tls)
89 #:use-module (gnu packages xml)
90 #:use-module (guix packages)
91 #:use-module (guix download)
92 #:use-module (guix utils)
93 #:use-module (guix build-system gnu)
94 #:use-module (guix build-system trivial))
96 (define-public python-2.7
103 (uri (string-append "https://www.python.org/ftp/python/"
104 version "/Python-" version ".tar.xz"))
107 "1mqfcqp5y8r0bfyr7ppl74n0lig45p9mc4b8adlcpvj74rhfy8pj"))
108 (patches (search-patches "python-2.7-search-paths.patch"
109 "python-2-deterministic-build-info.patch"
110 "python-2.7-site-prefixes.patch"
111 "python-2.7-source-date-epoch.patch"
112 "python-2.7-adjust-tests.patch"))
113 (modules '((guix build utils)))
116 ;; Ensure the bundled copies of these libraries are not used.
117 (for-each delete-file-recursively
118 '("Modules/_ctypes/libffi" "Modules/expat" "Modules/zlib"))
120 (substitute* "Modules/Setup.dist"
121 ;; Link Expat instead of embedding the bundled one.
122 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
124 ;; Suboptimal to delete failing tests here, but if we delete them in
125 ;; the arguments then we need to make sure to strip out that phase
126 ;; when it gets inherited by python and python-minimal.
127 (for-each delete-file
128 '("Lib/test/test_compileall.py"
129 "Lib/test/test_ctypes.py" ; fails on mips64el
130 "Lib/test/test_distutils.py"
131 "Lib/test/test_import.py"
132 "Lib/test/test_shutil.py"
133 "Lib/test/test_socket.py"
134 "Lib/test/test_subprocess.py"))
137 "tk")) ;tkinter; adds 50 MiB to the closure
138 (build-system gnu-build-system)
140 `(#:test-target "test"
142 (list "--enable-shared" ;allow embedding
143 "--with-system-expat" ;for XML support
144 "--with-system-ffi" ;build ctypes
145 "--with-ensurepip=install" ;install pip and setuptools
146 "--enable-unicode=ucs4"
148 ;; Prevent the installed _sysconfigdata.py from retaining a reference
153 ;; Disable runtime check failing if cross-compiling, see:
154 ;; https://lists.yoctoproject.org/pipermail/poky/2013-June/008997.html
155 ,@(if (%current-target-system)
156 '("ac_cv_buggy_getaddrinfo=no"
157 "ac_cv_file__dev_ptmx=no"
158 "ac_cv_file__dev_ptc=no")
160 (string-append "LDFLAGS=-Wl,-rpath="
161 (assoc-ref %outputs "out") "/lib"))
162 ;; With no -j argument tests use all available cpus, so provide one.
165 (format #f "TESTOPTS=-j~d" (parallel-job-count))
166 ;; Exclude the following tests as they fail
167 ;; non-deterministically with "error: [Errno 104] Connection
168 ;; reset by peer." Python 3 seems unaffected. A potential fix,
169 ;; yet to be backported to Python 2, is available at:
170 ;; https://github.com/python/cpython/commit/529525fb5a8fd9b96ab4021311a598c77588b918.
171 " --exclude test_urllib2_localnet test_httplib"))
173 #:modules ((ice-9 ftw) (ice-9 match)
174 (guix build utils) (guix build gnu-build-system))
176 (modify-phases %standard-phases
178 'configure 'patch-lib-shells
180 ;; Filter for existing files, since some may not exist in all
181 ;; versions of python that are built with this recipe.
182 (substitute* (filter file-exists?
183 '("Lib/subprocess.py"
185 "Lib/distutils/tests/test_spawn.py"
186 "Lib/test/support/__init__.py"
187 "Lib/test/test_subprocess.py"))
188 (("/bin/sh") (which "sh")))
190 (add-before 'configure 'do-not-record-configure-flags
191 (lambda* (#:key configure-flags #:allow-other-keys)
192 ;; Remove configure flags from the installed '_sysconfigdata.py'
193 ;; and 'Makefile' so we don't end up keeping references to the
196 ;; Preserve at least '--with-system-ffi' since otherwise the
197 ;; thing tries to build libffi, fails, and we end up with a
198 ;; Python that lacks ctypes.
199 (substitute* "configure"
200 (("^CONFIG_ARGS=.*$")
201 (format #f "CONFIG_ARGS='~a'\n"
202 (if (member "--with-system-ffi" configure-flags)
209 ;; 'Lib/test/test_site.py' needs a valid $HOME
210 (setenv "HOME" (getcwd))
213 'unpack 'set-source-file-times-to-1980
214 ;; XXX One of the tests uses a ZIP library to pack up some of the
215 ;; source tree, and fails with "ZIP does not support timestamps
216 ;; before 1980". Work around this by setting the file times in the
217 ;; source tree to sometime in early 1980.
219 (let ((circa-1980 (* 10 366 24 60 60)))
220 (ftw "." (lambda (file stat flag)
221 (utime file circa-1980 circa-1980)
224 (add-after 'install 'remove-tests
225 ;; Remove 25 MiB of unneeded unit tests. Keep test_support.*
226 ;; because these files are used by some libraries out there.
227 (lambda* (#:key outputs #:allow-other-keys)
228 (let ((out (assoc-ref outputs "out")))
229 (match (scandir (string-append out "/lib")
231 (string-prefix? "python" name)))
233 (let ((testdir (string-append out "/lib/" pythonX.Y
235 (with-directory-excursion testdir
236 (for-each delete-file-recursively
243 (string-prefix? "test_support."
245 (call-with-output-file "__init__.py" (const #t))
247 (add-after 'remove-tests 'rebuild-bytecode
248 (lambda* (#:key outputs #:allow-other-keys)
249 (let ((out (assoc-ref outputs "out")))
250 ;; Disable hash randomization to ensure the generated .pycs
252 (setenv "PYTHONHASHSEED" "0")
255 (format #t "Compiling with optimization level: ~a\n"
256 (if (null? opt) "none" (car opt)))
257 (for-each (lambda (file)
259 `(,(string-append out "/bin/python")
263 ;; Don't build lib2to3, because it contains Python 3 code.
266 (find-files out "\\.py$")))
267 (list '() '("-O") '("-OO")))
269 (add-after 'install 'move-tk-inter
270 (lambda* (#:key outputs #:allow-other-keys)
271 ;; When Tkinter support is built move it to a separate output so
272 ;; that the main output doesn't contain a reference to Tcl/Tk.
273 (let ((out (assoc-ref outputs "out"))
274 (tk (assoc-ref outputs "tk")))
276 (match (find-files out "tkinter.*\\.so")
278 ;; The .so is in OUT/lib/pythonX.Y/lib-dynload, but we
279 ;; want it under TK/lib/pythonX.Y/site-packages.
280 (let* ((len (string-length out))
281 (target (string-append
284 (dirname (dirname tkinter.so))
287 (install-file tkinter.so target)
288 (delete-file tkinter.so)))))
294 ("libffi" ,libffi) ; for ctypes
295 ("sqlite" ,sqlite) ; for sqlite extension
297 ("readline" ,readline)
300 ("tk" ,tk))) ; for tkinter
302 `(("pkg-config" ,pkg-config)
303 ;; When cross-compiling, a native version of Python itself is needed.
304 ,@(if (%current-target-system)
305 `(("self" ,this-package)
309 (list (search-path-specification
310 (variable "PYTHONPATH")
311 (files '("lib/python2.7/site-packages")))))
312 (home-page "https://www.python.org")
313 (synopsis "High-level, dynamically-typed programming language")
315 "Python is a remarkably powerful dynamic programming language that
316 is used in a wide variety of application domains. Some of its key
317 distinguishing features include: clear, readable syntax; strong
318 introspection capabilities; intuitive object orientation; natural
319 expression of procedural code; full modularity, supporting hierarchical
320 packages; exception-based error handling; and very high level dynamic
322 (properties '((cpe-name . "python")))
323 (license license:psfl)))
325 ;; Current 2.x version.
326 (define-public python-2 python-2.7)
328 (define-public python2-called-python
329 ;; Both 2.x and 3.x used to be called "python". In commit
330 ;; a7714d42de2c3082f3609d1e63c83d703fb39cf9 (March 2018), we renamed the
331 ;; Python 2.x package to "python2".
335 (properties `((superseded . ,python-2)))))
337 (define-public python-3.7
338 (package (inherit python-2)
343 (uri (string-append "https://www.python.org/ftp/python/"
344 version "/Python-" version ".tar.xz"))
345 (patches (search-patches
346 "python-3-fix-tests.patch"
347 "python-3-deterministic-build-info.patch"
348 "python-3-search-paths.patch"))
351 "0gxiv5617zd7dnqm5k9r4q2188lk327nf9jznwq9j6b8p0s92ygv"))
352 (modules '((guix build utils)))
355 ;; Delete the bundled copy of libexpat.
356 (delete-file-recursively "Modules/expat")
357 (substitute* "Modules/Setup.dist"
358 ;; Link Expat instead of embedding the bundled one.
359 (("^#pyexpat.*") "pyexpat pyexpat.c -lexpat\n"))
362 (substitute-keyword-arguments (package-arguments python-2)
364 `(list (string-append
365 (format #f "TESTOPTS=-j~d" (parallel-job-count))
366 ;; test_mmap fails on low-memory systems.
367 " --exclude test_mmap"
368 ;; test_socket may hang and eventually run out of memory
369 ;; on some systems: <https://bugs.python.org/issue34587>.
371 ;; XXX: test_ctypes fails on some platforms due to a problem in
372 ;; libffi 3.2.1: <https://bugs.python.org/issue23249>.
373 ,@(if (string-prefix? "aarch64" (%current-system))
377 `(modify-phases ,phases
378 (add-before 'check 'set-TZDIR
379 (lambda* (#:key inputs #:allow-other-keys)
380 ;; test_email requires the Olson time zone database.
382 (string-append (assoc-ref inputs "tzdata")
385 ;; Unset SOURCE_DATE_EPOCH while running the test-suite and set it
386 ;; again afterwards. See <https://bugs.python.org/issue34022>.
387 (add-before 'check 'unset-SOURCE_DATE_EPOCH
388 (lambda _ (unsetenv "SOURCE_DATE_EPOCH") #t))
389 (add-after 'check 'reset-SOURCE_DATE_EPOCH
390 (lambda _ (setenv "SOURCE_DATE_EPOCH" "1") #t))
391 (replace 'rebuild-bytecode
392 (lambda* (#:key outputs #:allow-other-keys)
393 (let ((out (assoc-ref outputs "out")))
394 ;; Disable hash randomization to ensure the generated .pycs
396 (setenv "PYTHONHASHSEED" "0")
399 (format #t "Compiling with optimization level: ~a\n"
400 (if (null? opt) "none" (car opt)))
401 (for-each (lambda (file)
403 `(,,(if (%current-target-system)
410 ;; Don't build lib2to3, because it's Python 2 code.
413 (find-files out "\\.py$")))
414 (list '() '("-O") '("-OO")))
417 `(("tzdata" ,tzdata-for-tests)
418 ,@(package-native-inputs python-2)))
420 (list (search-path-specification
421 (variable "PYTHONPATH")
422 (files (list (string-append "lib/python"
423 (version-major+minor version)
424 "/site-packages"))))))))
426 ;; Current 3.x version.
427 (define-public python-3 python-3.7)
429 ;; Current major version.
430 (define-public python python-3)
432 ;; Minimal variants of Python, mostly used to break the cycle between Tk and
433 ;; Python (Tk -> libxcb -> Python.)
435 (define-public python2-minimal
436 (package/inherit python-2
437 (name "python2-minimal")
440 ;; Keep zlib, which is used by 'pip' (via the 'zipimport' module), which
441 ;; is invoked upon 'make install'. 'pip' also expects 'ctypes' and thus
442 ;; libffi. Expat is needed for XML support which is expected by a lot
443 ;; of libraries out there.
444 (inputs `(("expat" ,expat)
448 (define-public python-minimal
449 (package/inherit python
450 (name "python-minimal")
453 ;; Build fails due to missing ctypes without libffi.
454 ;; OpenSSL is a mandatory dependency of Python 3.x, for urllib;
455 ;; zlib is required by 'zipimport', used by pip. Expat is needed
456 ;; for XML support, which is generally expected to be available.
457 (inputs `(("expat" ,expat)
462 (define-public python-debug
463 (package/inherit python
464 (name "python-debug")
465 (outputs '("out" "debug"))
466 (build-system gnu-build-system)
468 (substitute-keyword-arguments (package-arguments python)
469 ((#:configure-flags flags '())
470 `(cons "--with-pydebug" ,flags))))
472 "High-level, dynamically-typed programming language (for debugging)")
474 "This variant of Python provides an interpreter built with
475 @code{--with-pydebug} to help develop and debug extensions. See
476 @url{https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html},
477 for more information.")))
479 (define* (wrap-python3 python
481 (name (string-append (package-name python) "-wrapper")))
482 (package/inherit python
485 (build-system trivial-build-system)
487 (inputs `(("bash" ,bash)))
488 (propagated-inputs `(("python" ,python)))
490 `(#:modules ((guix build utils))
493 (use-modules (guix build utils))
494 (let ((bin (string-append (assoc-ref %outputs "out") "/bin"))
495 (python (string-append (assoc-ref %build-inputs "python") "/bin/")))
499 (symlink (string-append python old)
500 (string-append bin "/" new)))
501 `("python3" ,"pydoc3" ,"idle3" ,"pip3")
502 `("python" ,"pydoc" ,"idle" ,"pip"))
503 ;; python-config outputs search paths based upon its location,
504 ;; use a bash wrapper to avoid changing its outputs.
505 (let ((bash (string-append (assoc-ref %build-inputs "bash")
507 (old (string-append python "python3-config"))
508 (new (string-append bin "/python-config")))
509 (with-output-to-file new
511 (format #t "#!~a~%" bash)
512 (format #t "exec \"~a\" \"$@\"~%" old)
515 (synopsis "Wrapper for the Python 3 commands")
517 "This package provides wrappers for the commands of Python@tie{}3.x such
518 that they can be invoked under their usual name---e.g., @command{python}
519 instead of @command{python3}.")))
521 (define-public python-wrapper (wrap-python3 python))
522 (define-public python-minimal-wrapper (wrap-python3 python-minimal))
524 (define-public micropython
531 (uri (string-append "https://github.com/micropython/micropython/"
532 "releases/download/v" version
533 "/micropython-" version ".tar.gz"))
536 "0px3xhw16rl0l7qifq7jw1gq92wzlnhd17dmszv9m2c3wbzs9p9f"))
537 (modules '((guix build utils)))
540 (delete-file-recursively "ports/cc3200/FreeRTOS")
541 (with-directory-excursion "lib"
542 ;; TODO: Unbundle axtls and berkley-db-1.xx
543 (for-each delete-file-recursively
544 '("libffi" "lwip" "stm32lib" "nrfx")))
546 (build-system gnu-build-system)
549 (modify-phases %standard-phases
550 (add-before 'build 'prepare-build
553 ;; see: https://github.com/micropython/micropython/pull/4246
554 (substitute* "Makefile"
555 (("-Os") "-Os -ffp-contract=off"))
557 (replace 'install-license-files
558 ;; We don't build in the root directory so the file isn't found.
559 (lambda* (#:key outputs #:allow-other-keys)
560 (let* ((out (assoc-ref outputs "out"))
561 (dest (string-append out "/share/doc/" ,name "-" ,version "/")))
562 (install-file "../../LICENSE" dest))
564 (delete 'configure)) ; no configure
565 #:make-flags (list (string-append "PREFIX=" (assoc-ref %outputs "out"))
567 #:test-target "test"))
569 `(("pkg-config" ,pkg-config)
570 ("python" ,python-wrapper)))
572 `(("libffi" ,libffi)))
573 (home-page "https://micropython.org/")
574 (synopsis "Python implementation for microcontrollers and constrained systems")
575 (description "MicroPython is a lean and efficient implementation of the
576 Python 3 programming language that includes a small subset of the Python
577 standard library and is optimised to run on microcontrollers and in constrained
578 environments. MicroPython is packed full of advanced features such as an
579 interactive prompt, arbitrary precision integers, closures, list comprehension,
580 generators, exception handling and more. Still it is compact enough to fit and
581 run within just 256k of code space and 16k of RAM. MicroPython aims to be as
582 compatible with normal Python as possible to allow you to transfer code with
583 ease from the desktop to a microcontroller or embedded system.")
584 (license license:expat)))