gnu: r-mlinterfaces: Update to 1.62.1.
[jackhill/guix/guix.git] / gnu / packages / python.scm
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 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 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 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 <brendan.tildesley@openmailbox.org>
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 Maxim Cournoyer <maxim.cournoyer@gmail.com>
58 ;;; Copyright © 2018 Luther Thompson <lutheroto@gmail.com>
59 ;;; Copyright © 2018 Vagrant Cascadian <vagrant@debian.org>
60 ;;;
61 ;;; This file is part of GNU Guix.
62 ;;;
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.
67 ;;;
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.
72 ;;;
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/>.
75
76 (define-module (gnu packages python)
77 #:use-module ((guix licenses) #:prefix license:)
78 #:use-module (gnu packages)
79 #:use-module (gnu packages bash)
80 #:use-module (gnu packages compression)
81 #:use-module (gnu packages dbm)
82 #:use-module (gnu packages libffi)
83 #:use-module (gnu packages pkg-config)
84 #:use-module (gnu packages readline)
85 #:use-module (gnu packages sqlite)
86 #:use-module (gnu packages tcl)
87 #:use-module (gnu packages tls)
88 #:use-module (guix packages)
89 #:use-module (guix download)
90 #:use-module (guix utils)
91 #:use-module (guix build-system gnu)
92 #:use-module (guix build-system trivial))
93
94 (define-public python-2.7
95 (package
96 (name "python2")
97 (version "2.7.15")
98 (replacement python-2/fixed)
99 (source
100 (origin
101 (method url-fetch)
102 (uri (string-append "https://www.python.org/ftp/python/"
103 version "/Python-" version ".tar.xz"))
104 (sha256
105 (base32
106 "0x2mvz9dp11wj7p5ccvmk9s0hzjk2fa1m462p395l4r6bfnb3n92"))
107 (patches (search-patches "python-2.7-search-paths.patch"
108 "python-2-deterministic-build-info.patch"
109 "python-2.7-site-prefixes.patch"
110 "python-2.7-source-date-epoch.patch"
111 "python-2.7-adjust-tests.patch"))
112 (modules '((guix build utils)))
113 ;; suboptimal to delete failing tests here, but if we delete them in the
114 ;; arguments then we need to make sure to strip out that phase when it
115 ;; gets inherited by python and python-minimal.
116 (snippet
117 '(begin
118 (for-each delete-file
119 '("Lib/test/test_compileall.py"
120 "Lib/test/test_ctypes.py" ; fails on mips64el
121 "Lib/test/test_distutils.py"
122 "Lib/test/test_import.py"
123 "Lib/test/test_shutil.py"
124 "Lib/test/test_socket.py"
125 "Lib/test/test_subprocess.py"))
126 #t))))
127 (outputs '("out"
128 "tk")) ;tkinter; adds 50 MiB to the closure
129 (build-system gnu-build-system)
130 (arguments
131 `(#:test-target "test"
132 #:configure-flags
133 (list "--enable-shared" ;allow embedding
134 "--with-system-ffi" ;build ctypes
135 "--with-ensurepip=install" ;install pip and setuptools
136 "--enable-unicode=ucs4"
137 (string-append "LDFLAGS=-Wl,-rpath="
138 (assoc-ref %outputs "out") "/lib"))
139
140 #:modules ((ice-9 ftw) (ice-9 match)
141 (guix build utils) (guix build gnu-build-system))
142 #:phases
143 (modify-phases %standard-phases
144 (add-before
145 'configure 'patch-lib-shells
146 (lambda _
147 ;; Filter for existing files, since some may not exist in all
148 ;; versions of python that are built with this recipe.
149 (substitute* (filter file-exists?
150 '("Lib/subprocess.py"
151 "Lib/popen2.py"
152 "Lib/distutils/tests/test_spawn.py"
153 "Lib/test/support/__init__.py"
154 "Lib/test/test_subprocess.py"))
155 (("/bin/sh") (which "sh")))
156 #t))
157 (add-before 'configure 'do-not-record-configure-flags
158 (lambda* (#:key configure-flags #:allow-other-keys)
159 ;; Remove configure flags from the installed '_sysconfigdata.py'
160 ;; and 'Makefile' so we don't end up keeping references to the
161 ;; build tools.
162 ;;
163 ;; Preserve at least '--with-system-ffi' since otherwise the
164 ;; thing tries to build libffi, fails, and we end up with a
165 ;; Python that lacks ctypes.
166 (substitute* "configure"
167 (("^CONFIG_ARGS=.*$")
168 (format #f "CONFIG_ARGS='~a'\n"
169 (if (member "--with-system-ffi" configure-flags)
170 "--with-system-ffi"
171 ""))))
172 #t))
173 (add-before
174 'check 'pre-check
175 (lambda _
176 ;; 'Lib/test/test_site.py' needs a valid $HOME
177 (setenv "HOME" (getcwd))
178 #t))
179 (add-after
180 'unpack 'set-source-file-times-to-1980
181 ;; XXX One of the tests uses a ZIP library to pack up some of the
182 ;; source tree, and fails with "ZIP does not support timestamps
183 ;; before 1980". Work around this by setting the file times in the
184 ;; source tree to sometime in early 1980.
185 (lambda _
186 (let ((circa-1980 (* 10 366 24 60 60)))
187 (ftw "." (lambda (file stat flag)
188 (utime file circa-1980 circa-1980)
189 #t))
190 #t)))
191 (add-after 'install 'remove-tests
192 ;; Remove 25 MiB of unneeded unit tests. Keep test_support.*
193 ;; because these files are used by some libraries out there.
194 (lambda* (#:key outputs #:allow-other-keys)
195 (let ((out (assoc-ref outputs "out")))
196 (match (scandir (string-append out "/lib")
197 (lambda (name)
198 (string-prefix? "python" name)))
199 ((pythonX.Y)
200 (let ((testdir (string-append out "/lib/" pythonX.Y
201 "/test")))
202 (with-directory-excursion testdir
203 (for-each delete-file-recursively
204 (scandir testdir
205 (match-lambda
206 ((or "." "..") #f)
207 (file
208 (not
209 ;; FIXME: Add the 'support' directory
210 ;; in the next rebuild cycle, since it
211 ;; moved in 2.7.14. See also
212 ;; python2-futures below.
213 (string-prefix? "test_support."
214 file))))))
215 (call-with-output-file "__init__.py" (const #t))
216 #t)))))))
217 (add-after 'install 'move-tk-inter
218 (lambda* (#:key outputs #:allow-other-keys)
219 ;; When Tkinter support is built move it to a separate output so
220 ;; that the main output doesn't contain a reference to Tcl/Tk.
221 (let ((out (assoc-ref outputs "out"))
222 (tk (assoc-ref outputs "tk")))
223 (when tk
224 (match (find-files out "tkinter.*\\.so")
225 ((tkinter.so)
226 ;; The .so is in OUT/lib/pythonX.Y/lib-dynload, but we
227 ;; want it under TK/lib/pythonX.Y/site-packages.
228 (let* ((len (string-length out))
229 (target (string-append
230 tk "/"
231 (string-drop
232 (dirname (dirname tkinter.so))
233 len)
234 "/site-packages")))
235 (install-file tkinter.so target)
236 (delete-file tkinter.so)))))
237 #t))))))
238 (inputs
239 `(("bzip2" ,bzip2)
240 ("gdbm" ,gdbm)
241 ("libffi" ,libffi) ; for ctypes
242 ("sqlite" ,sqlite) ; for sqlite extension
243 ("openssl" ,openssl)
244 ("readline" ,readline)
245 ("zlib" ,zlib)
246 ("tcl" ,tcl)
247 ("tk" ,tk))) ; for tkinter
248 (native-inputs
249 `(("pkg-config" ,pkg-config)))
250 (native-search-paths
251 (list (search-path-specification
252 (variable "PYTHONPATH")
253 (files '("lib/python2.7/site-packages")))))
254 (home-page "https://www.python.org")
255 (synopsis "High-level, dynamically-typed programming language")
256 (description
257 "Python is a remarkably powerful dynamic programming language that
258 is used in a wide variety of application domains. Some of its key
259 distinguishing features include: clear, readable syntax; strong
260 introspection capabilities; intuitive object orientation; natural
261 expression of procedural code; full modularity, supporting hierarchical
262 packages; exception-based error handling; and very high level dynamic
263 data types.")
264 (license license:psfl)))
265
266 ;; Current 2.x version.
267 (define-public python-2 python-2.7)
268
269 (define python-2/fixed
270 (package
271 (inherit python-2)
272 (source (origin
273 (inherit (package-source python-2))
274 (patches (append
275 (origin-patches (package-source python-2))
276 (search-patches "python2-CVE-2018-14647.patch"
277 "python2-CVE-2018-1000802.patch")))))))
278
279 (define-public python2-called-python
280 ;; Both 2.x and 3.x used to be called "python". In commit
281 ;; a7714d42de2c3082f3609d1e63c83d703fb39cf9 (March 2018), we renamed the
282 ;; Python 2.x package to "python2".
283 (package
284 (inherit python-2)
285 (name "python")
286 (properties `((superseded . ,python-2)))))
287
288 (define-public python-3.7
289 (package (inherit python-2)
290 (name "python")
291 (version "3.7.0")
292 (replacement python-3/fixed)
293 (source (origin
294 (method url-fetch)
295 (uri (string-append "https://www.python.org/ftp/python/"
296 version "/Python-" version ".tar.xz"))
297 (patches (search-patches
298 "python-fix-tests.patch"
299 "python-3-fix-tests.patch"
300 "python-3-deterministic-build-info.patch"
301 "python-3-search-paths.patch"))
302 (patch-flags '("-p0"))
303 (sha256
304 (base32
305 "0j9mic5c9lbd2b20wka7hily7szz740wy9ilfrczxap63rnrk0h3"))
306 (snippet
307 '(begin
308 (for-each delete-file
309 '(;; This test may hang and eventually run out of
310 ;; memory on some systems:
311 ;; <https://bugs.python.org/issue34587>
312 "Lib/test/test_socket.py"
313
314 ;; These tests fail on AArch64.
315 "Lib/ctypes/test/test_win32.py"
316 "Lib/test/test_fcntl.py"
317 "Lib/test/test_posix.py"))
318 #t))))
319 (arguments
320 (substitute-keyword-arguments (package-arguments python-2)
321 ((#:phases phases)
322 `(modify-phases ,phases
323 ;; Unset SOURCE_DATE_EPOCH while running the test-suite and set it
324 ;; again afterwards. See <https://bugs.python.org/issue34022>.
325 (add-before 'check 'unset-SOURCE_DATE_EPOCH
326 (lambda _ (unsetenv "SOURCE_DATE_EPOCH") #t))
327 (add-after 'check 'reset-SOURCE_DATE_EPOCH
328 (lambda _ (setenv "SOURCE_DATE_EPOCH" "1") #t))
329 ;; FIXME: Without this phase we have close to 400 files that
330 ;; differ across different builds of this package. With this phase
331 ;; there are 44 files left that differ.
332 (add-after 'remove-tests 'rebuild-bytecode
333 (lambda* (#:key outputs #:allow-other-keys)
334 (let ((out (assoc-ref outputs "out")))
335 ;; Disable hash randomization to ensure the generated .pycs
336 ;; are reproducible.
337 (setenv "PYTHONHASHSEED" "0")
338 (for-each
339 (lambda (opt)
340 (format #t "Compiling with optimization level: ~a\n"
341 (if (null? opt) "none" (car opt)))
342 (for-each (lambda (file)
343 (apply invoke
344 `(,(string-append out "/bin/python3")
345 ,@opt
346 "-m" "compileall"
347 "-f" ; force rebuild
348 ;; Don't build lib2to3, because it's Python 2 code.
349 "-x" "lib2to3/.*"
350 ,file)))
351 (find-files out "\\.py$")))
352 (list '() '("-O") '("-OO")))
353 #t)))))))
354 (native-search-paths
355 (list (search-path-specification
356 (variable "PYTHONPATH")
357 (files (list (string-append "lib/python"
358 (version-major+minor version)
359 "/site-packages"))))))))
360
361 ;; Current 3.x version.
362 (define-public python-3 python-3.7)
363
364 (define python-3/fixed
365 (package
366 (inherit python-3)
367 (source (origin
368 (inherit (package-source python-3))
369 (patches (append (origin-patches (package-source python-3))
370 (search-patches "python-CVE-2018-14647.patch")))))))
371
372 ;; Current major version.
373 (define-public python python-3)
374
375 ;; Minimal variants of Python, mostly used to break the cycle between Tk and
376 ;; Python (Tk -> libxcb -> Python.)
377
378 (define-public python2-minimal
379 (package/inherit python-2
380 (name "python2-minimal")
381 (outputs '("out"))
382
383 ;; Keep zlib, which is used by 'pip' (via the 'zipimport' module), which
384 ;; is invoked upon 'make install'. 'pip' also expects 'ctypes' and thus
385 ;; libffi.
386 (inputs `(("libffi" ,libffi)
387 ("zlib" ,zlib)))))
388
389 (define-public python-minimal
390 (package/inherit python
391 (name "python-minimal")
392 (outputs '("out"))
393
394 ;; Build fails due to missing ctypes without libffi.
395 ;; OpenSSL is a mandatory dependency of Python 3.x, for urllib;
396 ;; zlib is required by 'zipimport', used by pip.
397 (inputs `(("libffi" ,libffi)
398 ("openssl" ,openssl)
399 ("zlib" ,zlib)))))
400
401 (define-public python-debug
402 (package/inherit python
403 (name "python-debug")
404 (outputs '("out" "debug"))
405 (build-system gnu-build-system)
406 (arguments
407 (substitute-keyword-arguments (package-arguments python)
408 ((#:configure-flags flags '())
409 `(cons "--with-pydebug" ,flags))))
410 (synopsis
411 "High-level, dynamically-typed programming language (for debugging)")
412 (description
413 "This variant of Python provides an interpreter built with
414 @code{--with-pydebug} to help develop and debug extensions. See
415 @url{https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html},
416 for more information.")))
417
418 (define* (wrap-python3 python
419 #:optional
420 (name (string-append (package-name python) "-wrapper")))
421 (package/inherit python
422 (name name)
423 (source #f)
424 (build-system trivial-build-system)
425 (outputs '("out"))
426 (inputs `(("bash" ,bash)))
427 (propagated-inputs `(("python" ,python)))
428 (arguments
429 `(#:modules ((guix build utils))
430 #:builder
431 (begin
432 (use-modules (guix build utils))
433 (let ((bin (string-append (assoc-ref %outputs "out") "/bin"))
434 (python (string-append (assoc-ref %build-inputs "python") "/bin/")))
435 (mkdir-p bin)
436 (for-each
437 (lambda (old new)
438 (symlink (string-append python old)
439 (string-append bin "/" new)))
440 `("python3" ,"pydoc3" ,"idle3" ,"pip3")
441 `("python" ,"pydoc" ,"idle" ,"pip"))
442 ;; python-config outputs search paths based upon its location,
443 ;; use a bash wrapper to avoid changing its outputs.
444 (let ((bash (string-append (assoc-ref %build-inputs "bash")
445 "/bin/bash"))
446 (old (string-append python "python3-config"))
447 (new (string-append bin "/python-config")))
448 (with-output-to-file new
449 (lambda ()
450 (format #t "#!~a~%" bash)
451 (format #t "exec \"~a\" \"$@\"~%" old)
452 (chmod new #o755)
453 #t)))))))
454 (synopsis "Wrapper for the Python 3 commands")
455 (description
456 "This package provides wrappers for the commands of Python@tie{}3.x such
457 that they can be invoked under their usual name---e.g., @command{python}
458 instead of @command{python3}.")))
459
460 (define-public python-wrapper (wrap-python3 python))
461 (define-public python-minimal-wrapper (wrap-python3 python-minimal))
462
463 (define-public micropython
464 (package
465 (name "micropython")
466 (version "1.10")
467 (source
468 (origin
469 (method url-fetch)
470 (uri (string-append "https://github.com/micropython/micropython/"
471 "releases/download/v" version
472 "/micropython-" version ".tar.gz"))
473 (sha256
474 (base32
475 "1g1zjip3rkx6bp16qi1bag72wivnbh56fcsl3nffanrx4j5f4z90"))
476 (modules '((guix build utils)))
477 (snippet
478 '(begin
479 (delete-file-recursively "ports/cc3200/FreeRTOS")
480 (with-directory-excursion "lib"
481 ;; TODO: Unbundle axtls and berkley-db-1.xx
482 (for-each delete-file-recursively
483 '("libffi" "lwip" "stm32lib" "nrfx")))
484 #t))))
485 (build-system gnu-build-system)
486 (arguments
487 `(#:phases
488 (modify-phases %standard-phases
489 (add-before 'build 'preprare-build
490 (lambda _
491 (chdir "ports/unix")
492 ;; see: https://github.com/micropython/micropython/pull/4246
493 (substitute* "Makefile"
494 (("-Os") "-Os -ffp-contract=off"))
495 #t))
496 (replace 'install-license-files
497 ;; We don't build in the root directory so the file isn't found.
498 (lambda* (#:key outputs #:allow-other-keys)
499 (let* ((out (assoc-ref outputs "out"))
500 (dest (string-append out "/share/doc/" ,name "-" ,version "/")))
501 (install-file "../../LICENSE" dest))
502 #t))
503 (delete 'configure)) ; no configure
504 #:make-flags (list (string-append "PREFIX=" (assoc-ref %outputs "out"))
505 "V=1")
506 #:test-target "test"))
507 (native-inputs
508 `(("pkg-config" ,pkg-config)
509 ("python" ,python-wrapper)))
510 (inputs
511 `(("libffi" ,libffi)))
512 (home-page "https://micropython.org/")
513 (synopsis "Python implementation for microcontrollers and constrained systems")
514 (description "MicroPython is a lean and efficient implementation of the
515 Python 3 programming language that includes a small subset of the Python
516 standard library and is optimised to run on microcontrollers and in constrained
517 environments. MicroPython is packed full of advanced features such as an
518 interactive prompt, arbitrary precision integers, closures, list comprehension,
519 generators, exception handling and more. Still it is compact enough to fit and
520 run within just 256k of code space and 16k of RAM. MicroPython aims to be as
521 compatible with normal Python as possible to allow you to transfer code with
522 ease from the desktop to a microcontroller or embedded system.")
523 (license license:expat)))