git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #13700 from FRidh/python-docs

Documentation: rewrite Python

commitdf7930370316d16d0f264288a4f1eb7fe17e42ff authorFrederik Rietdijk <freddyrietdijk@fridh.nl> dateWed 30 Mar 2016, 08:35 (11 years ago) parente7ca6260ed331c
3 files changed+718−447
doc/default.nix+4 doc/languages-frameworks/python.md+714 doc/languages-frameworks/python.xml−447
doc/default.nix+4
@@ -47,6 +47,10 @@ stdenv.mkDerivation {
4747 outputFile = "introduction.xml";
4848 useChapters = true;
4949 }
50+ + toDocbook {
51+ inputFile = ./languages-frameworks/python.md;
52+ outputFile = "./languages-frameworks/python.xml";
53+ }
5054 + toDocbook {
5155 inputFile = ./haskell-users-guide.md;
5256 outputFile = "haskell-users-guide.xml";
doc/languages-frameworks/python.mdnew file+714
@@ -0,0 +1,714 @@
1+# Python
2+
3+## User Guide
4+
5+Several versions of Python are available on Nix as well as a high amount of
6+packages. The default interpreter is CPython 2.7.
7+
8+### Using Python
9+
10+#### Installing Python and packages
11+
12+It is important to make a distinction between Python packages that are
13+used as libraries, and applications that are written in Python.
14+
15+Applications on Nix are installed typically into your user
16+profile imperatively using `nix-env -i`, and on NixOS declaratively by adding the
17+package name to `environment.systemPackages` in `/etc/nixos/configuration.nix`.
18+Dependencies such as libraries are automatically installed and should not be
19+installed explicitly.
20+
21+The same goes for Python applications and libraries. Python applications can be
22+installed in your profile, but Python libraries you would like to use to develop
23+cannot. If you do install libraries in your profile, then you will end up with
24+import errors.
25+
26+#### Python environments using `nix-shell`
27+
28+The recommended method for creating Python environments for development is with
29+`nix-shell`. Executing
30+
31+```sh
32+$ nix-shell -p python35Packages.numpy python35Packages.toolz
33+```
34+
35+opens a Nix shell which has available the requested packages and dependencies.
36+Now you can launch the Python interpreter (which is itself a dependency)
37+
38+```sh
39+[nix-shell:~] python3
40+```
41+
42+If the packages were not available yet in the Nix store, Nix would download or
43+build them automatically. A convenient option with `nix-shell` is the `--run`
44+option, with which you can execute a command in the `nix-shell`. Let's say we
45+want the above environment and directly run the Python interpreter
46+
47+```sh
48+$ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3"
49+```
50+
51+This way you can use the `--run` option also to directly run a script
52+
53+```sh
54+$ nix-shell -p python35Packages.numpy python35Packages.toolz --run "python3 myscript.py"
55+```
56+
57+In fact, for this specific use case there is a more convenient method. You can
58+add a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) to your script
59+specifying which dependencies Nix shell needs. With the following shebang, you
60+can use `nix-shell myscript.py` and it will make available all dependencies and
61+run the script in the `python3` shell.
62+
63+```py
64+#! /usr/bin/env nix-shell
65+#! nix-shell -i python3 -p python3Packages.numpy
66+
67+import numpy
68+
69+print(numpy.__version__)
70+```
71+
72+Likely you do not want to type your dependencies each and every time. What you
73+can do is write a simple Nix expression which sets up an environment for you,
74+requiring you only to type `nix-shell`. Say we want to have Python 3.5, `numpy`
75+and `toolz`, like before, in an environment. With a `shell.nix` file
76+containing
77+
78+```nix
79+with import <nixpkgs> {};
80+
81+(pkgs.python35.buildEnv.override {
82+ extraLibs = with pkgs.python35Packages; [ numpy toolz ];
83+}).env
84+```
85+executing `nix-shell` gives you again a Nix shell from which you can run Python.
86+
87+What's happening here?
88+
89+1. We begin with importing the Nix Packages collections. `import <nixpkgs>` import the `<nixpkgs>` function, `{}` calls it and the `with` statement brings all attributes of `nixpkgs` in the local scope. Therefore we can now use `pkgs`.
90+2. Then we create a Python 3.5 environment with `pkgs.buildEnv`. Because we want to use it with a custom set of Python packages, we override it.
91+3. The `extraLibs` argument of the original `buildEnv` function can be used to specify which packages should be included. We want `numpy` and `toolz`. Again, we use the `with` statement to bring a set of attributes into the local scope.
92+4. And finally, for in interactive use we return the environment.
93+
94+### Developing with Python
95+
96+
97+Now that you know how to get a working Python environment on Nix, it is time to go forward and start actually developing with Python.
98+We will first have a look at how Python packages are packaged on Nix. Then, we will look how you can use development mode with your code.
99+
100+#### Python packaging on Nix
101+
102+On Nix all packages are built by functions. The main function in Nix for building Python packages is [`buildPythonPackage`](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix).
103+Let's see how we would build the `toolz` package. According to [`python-packages.nix`](https://raw.githubusercontent.com/NixOS/nixpkgs/master/pkgs/top-level/python-packages.nix) `toolz` is build using
104+
105+```nix
106+toolz = buildPythonPackage rec{
107+ name = "toolz-${version}";
108+ version = "0.7.4";
109+
110+ src = pkgs.fetchurl{
111+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
112+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
113+ };
114+
115+ meta = {
116+ homepage = "http://github.com/pytoolz/toolz/";
117+ description = "List processing tools and functional utilities";
118+ license = licenses.bsd3;
119+ maintainers = with maintainers; [ fridh ];
120+ };
121+};
122+```
123+
124+What happens here? The function `buildPythonPackage` is called and as argument
125+it accepts a set. In this case the set is a recursive set ([`rec`](http://nixos.org/nix/manual/#sec-constructs)).
126+One of the arguments is the name of the package, which consists of a basename
127+(generally following the name on PyPi) and a version. Another argument, `src`
128+specifies the source, which in this case is fetched from an url. `fetchurl` not
129+only downloads the target file, but also validates its hash. Furthermore, we
130+specify some (optional) [meta information](http://nixos.org/nixpkgs/manual/#chap-meta).
131+
132+The output of the function is a derivation, which is an attribute with the name
133+`toolz` of the set `pythonPackages`. Actually, sets are created for all interpreter versions,
134+so `python27Packages`, `python34Packages`, `python35Packages` and `pypyPackages`.
135+
136+The above example works when you're directly working on
137+`pkgs/top-level/python-packages.nix` in the Nixpkgs repository. Often though,
138+you will want to test a Nix expression outside of the Nixpkgs tree. If you
139+create a `shell.nix` file with the following contents
140+
141+```nix
142+with import <nixpkgs> {};
143+
144+pkgs.python35Packages.buildPythonPackage rec {
145+ name = "toolz-${version}";
146+ version = "0.7.4";
147+
148+ src = pkgs.fetchurl{
149+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
150+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
151+ };
152+
153+ meta = {
154+ homepage = "http://github.com/pytoolz/toolz/";
155+ description = "List processing tools and functional utilities";
156+ license = licenses.bsd3;
157+ maintainers = with maintainers; [ fridh ];
158+ };
159+}
160+```
161+
162+and then execute `nix-shell` will result in an environment in which you can use
163+Python 3.5 and the `toolz` package. As you can see we had to explicitly mention
164+for which Python version we want to build a package.
165+
166+The above example considered only a single package. Generally you will want to use multiple packages.
167+If we create a `shell.nix` file with the following contents
168+
169+```nix
170+with import <nixpkgs> {};
171+
172+( let
173+ toolz = pkgs.python35Packages.buildPythonPackage rec {
174+ name = "toolz-${version}";
175+ version = "0.7.4";
176+
177+ src = pkgs.fetchurl{
178+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
179+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
180+ };
181+
182+ meta = {
183+ homepage = "http://github.com/pytoolz/toolz/";
184+ description = "List processing tools and functional utilities";
185+ license = licenses.bsd3;
186+ maintainers = with maintainers; [ fridh ];
187+ };
188+ };
189+
190+ in pkgs.python35.buildEnv.override rec {
191+
192+ extraLibs = [ pkgs.python35Packages.numpy toolz ];
193+}
194+).env
195+```
196+
197+and again execute `nix-shell`, then we get a Python 3.5 environment with our
198+locally defined package as well as `numpy` which is build according to the
199+definition in Nixpkgs. What did we do here? Well, we took the Nix expression
200+that we used earlier to build a Python environment, and said that we wanted to
201+include our own version of `toolz`. To introduce our own package in the scope of
202+`buildEnv.override` we used a
203+[`let`](http://nixos.org/nix/manual/#sec-constructs) expression.
204+
205+### Handling dependencies
206+
207+Our example, `toolz`, doesn't have any dependencies on other Python
208+packages or system libraries. According to the manual, `buildPythonPackage`
209+uses the arguments `buildInputs` and `propagatedBuildInputs` to specify dependencies. If something is
210+exclusively a build-time dependency, then the dependency should be included as a
211+`buildInput`, but if it is (also) a runtime dependency, then it should be added
212+to `propagatedBuildInputs`. Test dependencies are considered build-time dependencies.
213+
214+The following example shows which arguments are given to `buildPythonPackage` in
215+order to build [`datashape`](https://github.com/blaze/datashape).
216+
217+```nix
218+datashape = buildPythonPackage rec {
219+ name = "datashape-${version}";
220+ version = "0.4.7";
221+
222+ src = pkgs.fetchurl {
223+ url = "https://pypi.python.org/packages/source/D/DataShape/${name}.tar.gz";
224+ sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278";
225+ };
226+
227+ buildInputs = with self; [ pytest ];
228+ propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ];
229+
230+ meta = {
231+ homepage = https://github.com/ContinuumIO/datashape;
232+ description = "A data description language";
233+ license = licenses.bsd2;
234+ maintainers = with maintainers; [ fridh ];
235+ };
236+};
237+```
238+
239+We can see several runtime dependencies, `numpy`, `multipledispatch`, and
240+`dateutil`. Furthermore, we have one `buildInput`, i.e. `pytest`. `pytest` is a
241+test runner and is only used during the `checkPhase` and is therefore not added
242+to `propagatedBuildInputs`.
243+
244+In the previous case we had only dependencies on other Python packages to consider.
245+Occasionally you have also system libraries to consider. E.g., `lxml` provides
246+Python bindings to `libxml2` and `libxslt`. These libraries are only required
247+when building the bindings and are therefore added as `buildInputs`.
248+
249+```nix
250+lxml = buildPythonPackage rec {
251+ name = "lxml-3.4.4";
252+
253+ src = pkgs.fetchurl {
254+ url = "http://pypi.python.org/packages/source/l/lxml/${name}.tar.gz";
255+ sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk";
256+ };
257+
258+ buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ];
259+
260+ meta = {
261+ description = "Pythonic binding for the libxml2 and libxslt libraries";
262+ homepage = http://lxml.de;
263+ license = licenses.bsd3;
264+ maintainers = with maintainers; [ sjourdois ];
265+ };
266+};
267+```
268+
269+In this example `lxml` and Nix are able to work out exactly where the relevant
270+files of the dependencies are. This is not always the case.
271+
272+The example below shows bindings to The Fastest Fourier Transform in the West, commonly known as
273+FFTW. On Nix we have separate packages of FFTW for the different types of floats
274+(`"single"`, `"double"`, `"long-double"`). The bindings need all three types,
275+and therefore we add all three as `buildInputs`. The bindings don't expect to
276+find each of them in a different folder, and therefore we have to set `LDFLAGS`
277+and `CFLAGS`.
278+
279+```nix
280+pyfftw = buildPythonPackage rec {
281+ name = "pyfftw-${version}";
282+ version = "0.9.2";
283+
284+ src = pkgs.fetchurl {
285+ url = "https://pypi.python.org/packages/source/p/pyFFTW/pyFFTW-${version}.tar.gz";
286+ sha256 = "f6bbb6afa93085409ab24885a1a3cdb8909f095a142f4d49e346f2bd1b789074";
287+ };
288+
289+ buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble];
290+
291+ propagatedBuildInputs = with self; [ numpy scipy ];
292+
293+ # Tests cannot import pyfftw. pyfftw works fine though.
294+ doCheck = false;
295+
296+ LDFLAGS="-L${pkgs.fftw}/lib -L${pkgs.fftwFloat}/lib -L${pkgs.fftwLongDouble}/lib"
297+ CFLAGS="-I${pkgs.fftw}/include -I${pkgs.fftwFloat}/include -I${pkgs.fftwLongDouble}/include"
298+ '';
299+
300+ meta = {
301+ description = "A pythonic wrapper around FFTW, the FFT library, presenting a unified interface for all the supported transforms";
302+ homepage = http://hgomersall.github.com/pyFFTW/;
303+ license = with licenses; [ bsd2 bsd3 ];
304+ maintainer = with maintainers; [ fridh ];
305+ };
306+};
307+```
308+Note also the line `doCheck = false;`, we explicitly disabled running the test-suite.
309+
310+
311+#### Develop local package
312+
313+As a Python developer you're likely aware of [development mode](http://pythonhosted.org/setuptools/setuptools.html#development-mode) (`python setup.py develop`);
314+instead of installing the package this command creates a special link to the project code.
315+That way, you can run updated code without having to reinstall after each and every change you make.
316+Development mode is also available on Nix as [explained](http://nixos.org/nixpkgs/manual/#ssec-python-development) in the Nixpkgs manual.
317+Let's see how you can use it.
318+
319+In the previous Nix expression the source was fetched from an url. We can also refer to a local source instead using
320+
321+```nix
322+src = ./path/to/source/tree;
323+```
324+
325+If we create a `shell.nix` file which calls `buildPythonPackage`, and if `src`
326+is a local source, and if the local source has a `setup.py`, then development
327+mode is activated.
328+
329+In the following example we create a simple environment that
330+has a Python 3.5 version of our package in it, as well as its dependencies and
331+other packages we like to have in the environment, all specified with `propagatedBuildInputs`.
332+Indeed, we can just add any package we like to have in our environment to `propagatedBuildInputs`.
333+
334+```nix
335+with import <nixpkgs>;
336+with pkgs.python35Packages;
337+
338+buildPythonPackage rec {
339+ name = "mypackage";
340+ src = ./path/to/package/source;
341+ propagatedBuildInputs = [ pytest numpy pkgs.libsndfile ];
342+};
343+```
344+
345+It is important to note that due to how development mode is implemented on Nix it is not possible to have multiple packages simultaneously in development mode.
346+
347+
348+### Organising your packages
349+
350+So far we discussed how you can use Python on Nix, and how you can develop with
351+it. We've looked at how you write expressions to package Python packages, and we
352+looked at how you can create environments in which specified packages are
353+available.
354+
355+At some point you'll likely have multiple packages which you would
356+like to be able to use in different projects. In order to minimise unnecessary
357+duplication we now look at how you can maintain yourself a repository with your
358+own packages. The important functions here are `import` and `callPackage`.
359+
360+### Including a derivation using `callPackage`
361+
362+Earlier we created a Python environment using `buildEnv`, and included the
363+`toolz` package via a `let` expression.
364+Let's split the package definition from the environment definition.
365+
366+We first create a function that builds `toolz` in `~/path/to/toolz/release.nix`
367+
368+```nix
369+{ pkgs, buildPythonPackage }:
370+
371+buildPythonPackage rec {
372+ name = "toolz-${version}";
373+ version = "0.7.4";
374+
375+ src = pkgs.fetchurl{
376+ url = "https://pypi.python.org/packages/source/t/toolz/toolz-${version}.tar.gz";
377+ sha256 = "43c2c9e5e7a16b6c88ba3088a9bfc82f7db8e13378be7c78d6c14a5f8ed05afd";
378+ };
379+
380+ meta = {
381+ homepage = "http://github.com/pytoolz/toolz/";
382+ description = "List processing tools and functional utilities";
383+ license = licenses.bsd3;
384+ maintainers = with maintainers; [ fridh ];
385+ };
386+};
387+```
388+
389+It takes two arguments, `pkgs` and `buildPythonPackage`.
390+We now call this function using `callPackage` in the definition of our environment
391+
392+```nix
393+with import <nixpkgs> {};
394+
395+( let
396+ toolz = pkgs.callPackage ~/path/to/toolz/release.nix { pkgs=pkgs; buildPythonPackage=pkgs.python35Packages.buildPythonPackage; };
397+ in pkgs.python35.buildEnv.override rec {
398+ extraLibs = [ pkgs.python35Packages.numpy toolz ];
399+}
400+).env
401+```
402+
403+Important to remember is that the Python version for which the package is made
404+depends on the `python` derivation that is passed to `buildPythonPackage`. Nix
405+tries to automatically pass arguments when possible, which is why generally you
406+don't explicitly define which `python` derivation should be used. In the above
407+example we use `buildPythonPackage` that is part of the set `python35Packages`,
408+and in this case the `python35` interpreter is automatically used.
409+
410+
411+
412+## Reference
413+
414+### Interpreters
415+
416+Versions 2.6, 2.7, 3.3, 3.4 and 3.5 of the CPython interpreter are available on
417+Nix and are available as `python26`, `python27`, `python33`, `python34` and
418+`python35`. The PyPy interpreter is also available as `pypy`. Currently, the
419+aliases `python` and `python3` correspond to respectively `python27` and
420+`python35`. The Nix expressions for the interpreters can be found in
421+`pkgs/development/interpreters/python`.
422+
423+
424+#### Missing modules standard library
425+
426+The interpreters `python26` and `python27` do not include modules that
427+require external dependencies. This is done in order to reduce the closure size.
428+The following modules need to be added as `buildInput` explicitly:
429+
430+* `python.modules.bsddb`
431+* `python.modules.curses`
432+* `python.modules.curses_panel`
433+* `python.modules.crypt`
434+* `python.modules.gdbm`
435+* `python.modules.sqlite3`
436+* `python.modules.tkinter`
437+* `python.modules.readline`
438+
439+For convenience `python27Full` and `python26Full` are provided with all
440+modules included.
441+
442+All packages depending on any Python interpreter get appended
443+`out/{python.sitePackages}` to `$PYTHONPATH` if such directory
444+exists.
445+
446+#### Attributes on interpreters packages
447+
448+Each interpreter has the following attributes:
449+
450+- `libPrefix`. Name of the folder in `${python}/lib/` for corresponding interpreter.
451+- `interpreter`. Alias for `${python}/bin/${executable}`.
452+- `buildEnv`. Function to build python interpreter environments with extra packages bundled together. See section *python.buildEnv function* for usage and documentation.
453+- `sitePackages`. Alias for `lib/${libPrefix}/site-packages`.
454+- `executable`. Name of the interpreter executable, ie `python3.4`.
455+
456+### Building packages and applications
457+
458+Python packages (libraries) and applications that use `setuptools` or
459+`distutils` are typically built with respectively the `buildPythonPackage` and
460+`buildPythonApplication` functions.
461+
462+All Python packages reside in `pkgs/top-level/python-packages.nix` and all
463+applications elsewhere. Some packages are also defined in
464+`pkgs/development/python-modules`. It is important that these packages are
465+called in `pkgs/top-level/python-packages.nix` and not elsewhere, to guarantee
466+the right version of the package is built.
467+
468+Based on the packages defined in `pkgs/top-level/python-packages.nix` an
469+attribute set is created for each available Python interpreter. The available
470+sets are
471+
472+* `pkgs.python26Packages`
473+* `pkgs.python27Packages`
474+* `pkgs.python33Packages`
475+* `pkgs.python34Packages`
476+* `pkgs.python35Packages`
477+* `pkgs.pypyPackages`
478+
479+and the aliases
480+
481+* `pkgs.pythonPackages` pointing to `pkgs.python27Packages`
482+* `pkgs.python3Packages` pointing to `pkgs.python35Packages`
483+
484+#### `buildPythonPackage` function
485+
486+The `buildPythonPackage` function is implemented in
487+`pkgs/development/python-modules/generic/default.nix`
488+
489+and can be used as:
490+
491+ twisted = buildPythonPackage {
492+ name = "twisted-8.1.0";
493+
494+ src = pkgs.fetchurl {
495+ url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
496+ sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
497+ };
498+
499+ propagatedBuildInputs = [ self.ZopeInterface ];
500+
501+ meta = {
502+ homepage = http://twistedmatrix.com/;
503+ description = "Twisted, an event-driven networking engine written in Python";
504+ license = stdenv.lib.licenses.mit; };
505+ };
506+
507+The `buildPythonPackage` mainly does four things:
508+
509+* In the `buildPhase`, it calls `${python.interpreter} setup.py bdist_wheel` to build a wheel binary zipfile.
510+* In the `installPhase`, it installs the wheel file using `pip install *.whl`.
511+* In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to wrap all programs in the `$out/bin/*` directory to include `$PYTHONPATH` and `$PATH` environment variables.
512+* In the `installCheck` phase, `${python.interpreter} setup.py test` is ran.
513+
514+As in Perl, dependencies on other Python packages can be specified in the
515+`buildInputs` and `propagatedBuildInputs` attributes. If something is
516+exclusively a build-time dependency, use `buildInputs`; if it’s (also) a runtime
517+dependency, use `propagatedBuildInputs`.
518+
519+By default tests are run because `doCheck = true`. Test dependencies, like
520+e.g. the test runner, should be added to `buildInputs`.
521+
522+By default `meta.platforms` is set to the same value
523+as the interpreter unless overriden otherwise.
524+
525+##### `buildPythonPackage` parameters
526+
527+All parameters from `mkDerivation` function are still supported.
528+
529+* `namePrefix`: Prepended text to `${name}` parameter. Defaults to `"python3.3-"` for Python 3.3, etc. Set it to `""` if you're packaging an application or a command line tool.
530+* `disabled`: If `true`, package is not build for particular python interpreter version. Grep around `pkgs/top-level/python-packages.nix` for examples.
531+* `setupPyBuildFlags`: List of flags passed to `setup.py build_ext` command.
532+* `pythonPath`: List of packages to be added into `$PYTHONPATH`. Packages in `pythonPath` are not propagated (contrary to `propagatedBuildInputs`).
533+* `preShellHook`: Hook to execute commands before `shellHook`.
534+* `postShellHook`: Hook to execute commands after `shellHook`.
535+* `makeWrapperArgs`: A list of strings. Arguments to be passed to `makeWrapper`, which wraps generated binaries. By default, the arguments to `makeWrapper` set `PATH` and `PYTHONPATH` environment variables before calling the binary. Additional arguments here can allow a developer to set environment variables which will be available when the binary is run. For example, `makeWrapperArgs = ["--set FOO BAR" "--set BAZ QUX"]`.
536+* `installFlags`: A list of strings. Arguments to be passed to `pip install`. To pass options to `python setup.py install`, use `--install-option`. E.g., `installFlags=["--install-option='--cpp_implementation'"].
537+
538+#### `buildPythonApplication` function
539+
540+The `buildPythonApplication` function is practically the same as `buildPythonPackage`.
541+The difference is that `buildPythonPackage` by default prefixes the names of the packages with the version of the interpreter.
542+Because with an application we're not interested in multiple version the prefix is dropped.
543+
544+#### python.buildEnv function
545+
546+Python environments can be created using the low-level `pkgs.buildEnv` function.
547+This example shows how to create an environment that has the Pyramid Web Framework.
548+Saving the following as `default.nix`
549+
550+ with import {};
551+
552+ python.buildEnv.override {
553+ extraLibs = [ pkgs.pythonPackages.pyramid ];
554+ ignoreCollisions = true;
555+ }
556+
557+and running `nix-build` will create
558+
559+ /nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env
560+
561+with wrapped binaries in `bin/`.
562+
563+You can also use the `env` attribute to create local environments with needed
564+packages installed. This is somewhat comparable to `virtualenv`. For example,
565+running `nix-shell` with the following `shell.nix`
566+
567+ with import {};
568+
569+ (python3.buildEnv.override {
570+ extraLibs = with python3Packages; [ numpy requests ];
571+ }).env
572+
573+will drop you into a shell where Python will have the
574+specified packages in its path.
575+
576+
577+##### `python.buildEnv` arguments
578+
579+* `extraLibs`: List of packages installed inside the environment.
580+* `postBuild`: Shell command executed after the build of environment.
581+* `ignoreCollisions`: Ignore file collisions inside the environment (default is `false`).
582+
583+### Development mode
584+
585+Development or editable mode is supported. To develop Python packages
586+`buildPythonPackage` has additional logic inside `shellPhase` to run `pip
587+install -e . --prefix $TMPDIR/`for the package.
588+
589+Warning: `shellPhase` is executed only if `setup.py` exists.
590+
591+Given a `default.nix`:
592+
593+ with import {};
594+
595+ buildPythonPackage { name = "myproject";
596+
597+ buildInputs = with pkgs.pythonPackages; [ pyramid ];
598+
599+ src = ./.; }
600+
601+Running `nix-shell` with no arguments should give you
602+the environment in which the package would be build with
603+`nix-build`.
604+
605+Shortcut to setup environments with C headers/libraries and python packages:
606+
607+ $ nix-shell -p pythonPackages.pyramid zlib libjpeg git
608+
609+Note: There is a boolean value `lib.inNixShell` set to `true` if nix-shell is invoked.
610+
611+### Tools
612+
613+Packages inside nixpkgs are written by hand. However many tools exist in
614+community to help save time. No tool is preferred at the moment.
615+
616+- [python2nix](https://github.com/proger/python2nix) by Vladimir Kirillov
617+- [pypi2nix](https://github.com/garbas/pypi2nix) by Rok Garbas
618+- [pypi2nix](https://github.com/offlinehacker/pypi2nix) by Jaka Hudoklin
619+
620+## FAQ
621+
622+### How to solve circular dependencies?
623+
624+Consider the packages `A` and `B` that depend on each other. When packaging `B`,
625+a solution is to override package `A` not to depend on `B` as an input. The same
626+should also be done when packaging `A`.
627+
628+### How to override a Python package?
629+
630+Recursively updating a package can be done with `pkgs.overridePackages` as explained in the Nixpkgs manual.
631+Python attribute sets are created for each interpreter version. We will therefore override the attribute set for the interpreter version we're interested.
632+In the following example we change the name of the package `pandas` to `foo`.
633+```
634+newpkgs = pkgs.overridePackages(self: super: rec {
635+ python35Packages = super.python35Packages.override {
636+ self = python35Packages // { pandas = python35Packages.pandas.override{name="foo";};};
637+ };
638+});
639+```
640+This can be tested with
641+```
642+with import <nixpkgs> {};
643+
644+(let
645+
646+newpkgs = pkgs.overridePackages(self: super: rec {
647+ python35Packages = super.python35Packages.override {
648+ self = python35Packages // { pandas = python35Packages.pandas.override{name="foo";};};
649+ };
650+});
651+in newpkgs.python35.buildEnv.override{
652+ extraLibs = [newpkgs.python35Packages.blaze ];
653+}).env
654+```
655+A typical use case is to switch to another version of a certain package. For example, in the Nixpkgs repository we have multiple versions of `django` and `scipy`.
656+In the following example we use a different version of `scipy`. All packages in `newpkgs` will now use the updated `scipy` version.
657+```
658+with import <nixpkgs> {};
659+
660+(let
661+
662+newpkgs = pkgs.overridePackages(self: super: rec {
663+ python35Packages = super.python35Packages.override {
664+ self = python35Packages // { scipy = python35Packages.scipy_0_16;};
665+ };
666+});
667+in pkgs.python35.buildEnv.override{
668+ extraLibs = [newpkgs.python35Packages.blaze ];
669+}).env
670+```
671+The requested package `blaze` depends upon `pandas` which itself depends on `scipy`.
672+
673+
674+### `install_data` / `data_files` problems
675+
676+If you get the following error:
677+
678+ could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc':
679+ Permission denied
680+
681+This is a [known bug](https://bitbucket.org/pypa/setuptools/issue/130/install_data-doesnt-respect-prefix) in setuptools.
682+Setuptools `install_data` does not respect `--prefix`. An example of such package using the feature is `pkgs/tools/X11/xpra/default.nix`.
683+As workaround install it as an extra `preInstall` step:
684+
685+ ${python.interpreter} setup.py install_data --install-dir=$out --root=$out
686+ sed -i '/ = data\_files/d' setup.py
687+
688+
689+### Rationale of non-existent global site-packages
690+
691+On most operating systems a global `site-packages` is maintained. This however
692+becomes problematic if you want to run multiple Python versions or have multiple
693+versions of certain libraries for your projects. Generally, you would solve such
694+issues by creating virtual environments using `virtualenv`.
695+
696+On Nix each package has an isolated dependency tree which, in the case of
697+Python, guarantees the right versions of the interpreter and libraries or
698+packages are available. There is therefore no need to maintain a global `site-packages`.
699+
700+If you want to create a Python environment for development, then the recommended
701+method is to use `nix-shell`, either with or without the `python.buildEnv`
702+function.
703+
704+
705+## Contributing
706+
707+### Contributing guidelines
708+
709+Following rules are desired to be respected:
710+
711+* Make sure package builds for all python interpreters. Use `disabled` argument to `buildPythonPackage` to set unsupported interpreters.
712+* If tests need to be disabled for a package, make sure you leave a comment about reasoning.
713+* Packages in `pkgs/top-level/python-packages.nix` are sorted quasi-alphabetically to avoid merge conflicts.
714+* Python libraries are supposed to be in `python-packages.nix` and packaged with `buildPythonPackage`. Python applications live outside of `python-packages.nix` and are packaged with `buildPythonApplication`.
doc/languages-frameworks/python.xmldeleted−447
@@ -1,447 +0,0 @@
1−<section xmlns="http://docbook.org/ns/docbook"
2− xmlns:xlink="http://www.w3.org/1999/xlink"
3− xml:id="sec-python">
4−
5−<title>Python</title>
6−
7−<para>
8− Currently supported interpreters are <varname>python26</varname>, <varname>python27</varname>,
9− <varname>python33</varname>, <varname>python34</varname>, <varname>python35</varname>
10− and <varname>pypy</varname>.
11−</para>
12−
13−<para>
14− <varname>python</varname> is an alias to <varname>python27</varname> and <varname>python3</varname> is an alias to <varname>python34</varname>.
15−</para>
16−
17−<para>
18− <varname>python26</varname> and <varname>python27</varname> do not include modules that require
19− external dependencies (to reduce dependency bloat). Following modules need to be added as
20− <varname>buildInput</varname> explicitly:
21−</para>
22−
23−<itemizedlist>
24− <listitem><para><varname>python.modules.bsddb</varname></para></listitem>
25− <listitem><para><varname>python.modules.curses</varname></para></listitem>
26− <listitem><para><varname>python.modules.curses_panel</varname></para></listitem>
27− <listitem><para><varname>python.modules.crypt</varname></para></listitem>
28− <listitem><para><varname>python.modules.gdbm</varname></para></listitem>
29− <listitem><para><varname>python.modules.sqlite3</varname></para></listitem>
30− <listitem><para><varname>python.modules.tkinter</varname></para></listitem>
31− <listitem><para><varname>python.modules.readline</varname></para></listitem>
32−</itemizedlist>
33−
34−<para>For convenience <varname>python27Full</varname> and <varname>python26Full</varname>
35−are provided with all modules included.</para>
36−
37−<para>
38− Python packages that
39− use <link xlink:href="http://pypi.python.org/pypi/setuptools/"><literal>setuptools</literal></link> or <literal>distutils</literal>,
40− can be built using the <varname>buildPythonPackage</varname> function as documented below.
41−</para>
42−
43−<para>
44− All packages depending on any Python interpreter get appended <varname>$out/${python.sitePackages}</varname>
45− to <literal>$PYTHONPATH</literal> if such directory exists.
46−</para>
47−
48−<variablelist>
49− <title>
50− Useful attributes on interpreters packages:
51− </title>
52−
53− <varlistentry>
54− <term><varname>libPrefix</varname></term>
55− <listitem><para>
56− Name of the folder in <literal>${python}/lib/</literal> for corresponding interpreter.
57− </para></listitem>
58− </varlistentry>
59−
60− <varlistentry>
61− <term><varname>interpreter</varname></term>
62− <listitem><para>
63− Alias for <literal>${python}/bin/${executable}.</literal>
64− </para></listitem>
65− </varlistentry>
66−
67− <varlistentry>
68− <term><varname>buildEnv</varname></term>
69− <listitem><para>
70− Function to build python interpreter environments with extra packages bundled together.
71− See <xref linkend="ssec-python-build-env" /> for usage and documentation.
72− </para></listitem>
73− </varlistentry>
74−
75− <varlistentry>
76− <term><varname>sitePackages</varname></term>
77− <listitem><para>
78− Alias for <literal>lib/${libPrefix}/site-packages</literal>.
79− </para></listitem>
80− </varlistentry>
81−
82− <varlistentry>
83− <term><varname>executable</varname></term>
84− <listitem><para>
85− Name of the interpreter executable, ie <literal>python3.4</literal>.
86− </para></listitem>
87− </varlistentry>
88−
89−</variablelist>
90−<section xml:id="ssec-build-python-package"><title><varname>buildPythonPackage</varname> function</title>
91−
92− <para>
93− The function is implemented in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/generic/default.nix">
94− <filename>pkgs/development/python-modules/generic/default.nix</filename></link>.
95− Example usage:
96−
97−<programlisting language="nix">
98−twisted = buildPythonPackage {
99− name = "twisted-8.1.0";
100−
101− src = pkgs.fetchurl {
102− url = http://tmrc.mit.edu/mirror/twisted/Twisted/8.1/Twisted-8.1.0.tar.bz2;
103− sha256 = "0q25zbr4xzknaghha72mq57kh53qw1bf8csgp63pm9sfi72qhirl";
104− };
105−
106− propagatedBuildInputs = [ self.ZopeInterface ];
107−
108− meta = {
109− homepage = http://twistedmatrix.com/;
110− description = "Twisted, an event-driven networking engine written in Python";
111− license = stdenv.lib.licenses.mit;
112− };
113−};
114−</programlisting>
115−
116− Most of Python packages that use <varname>buildPythonPackage</varname> are defined
117− in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/python-packages.nix"><filename>pkgs/top-level/python-packages.nix</filename></link>
118− and generated for each python interpreter separately into attribute sets <varname>python26Packages</varname>,
119− <varname>python27Packages</varname>, <varname>python35Packages</varname>, <varname>python33Packages</varname>,
120− <varname>python34Packages</varname> and <varname>pypyPackages</varname>.
121− </para>
122−
123− <para>
124− <function>buildPythonPackage</function> mainly does four things:
125−
126− <orderedlist>
127− <listitem><para>
128− In the <varname>buildPhase</varname>, it calls
129− <literal>${python.interpreter} setup.py bdist_wheel</literal> to build a wheel binary zipfile.
130− </para></listitem>
131−
132− <listitem><para>
133− In the <varname>installPhase</varname>, it installs the wheel file using
134− <literal>pip install *.whl</literal>.
135− </para></listitem>
136−
137− <listitem><para>
138− In the <varname>postFixup</varname> phase, <literal>wrapPythonPrograms</literal>
139− bash function is called to wrap all programs in <filename>$out/bin/*</filename>
140− directory to include <literal>$PYTHONPATH</literal> and <literal>$PATH</literal>
141− environment variables.
142− </para></listitem>
143−
144− <listitem><para>
145− In the <varname>installCheck</varname> phase, <literal>${python.interpreter} setup.py test</literal>
146− is ran.
147− </para></listitem>
148− </orderedlist>
149− </para>
150−
151− <para>By default <varname>doCheck = true</varname> is set</para>
152−
153− <para>
154− As in Perl, dependencies on other Python packages can be specified in the
155− <varname>buildInputs</varname> and
156− <varname>propagatedBuildInputs</varname> attributes. If something is
157− exclusively a build-time dependency, use
158− <varname>buildInputs</varname>; if it’s (also) a runtime dependency,
159− use <varname>propagatedBuildInputs</varname>.
160− </para>
161−
162− <para>
163− By default <varname>meta.platforms</varname> is set to the same value
164− as the interpreter unless overriden otherwise.
165− </para>
166−
167− <variablelist>
168− <title>
169− <varname>buildPythonPackage</varname> parameters
170− (all parameters from <varname>mkDerivation</varname> function are still supported)
171− </title>
172−
173− <varlistentry>
174− <term><varname>namePrefix</varname></term>
175− <listitem><para>
176− Prepended text to <varname>${name}</varname> parameter.
177− Defaults to <literal>"python3.3-"</literal> for Python 3.3, etc. Set it to
178− <literal>""</literal>
179− if you're packaging an application or a command line tool.
180− </para></listitem>
181− </varlistentry>
182−
183− <varlistentry>
184− <term><varname>disabled</varname></term>
185− <listitem><para>
186− If <varname>true</varname>, package is not build for
187− particular python interpreter version. Grep around
188− <filename>pkgs/top-level/python-packages.nix</filename>
189− for examples.
190− </para></listitem>
191− </varlistentry>
192−
193− <varlistentry>
194− <term><varname>setupPyBuildFlags</varname></term>
195− <listitem><para>
196− List of flags passed to <command>setup.py build_ext</command> command.
197− </para></listitem>
198− </varlistentry>
199−
200− <varlistentry>
201− <term><varname>pythonPath</varname></term>
202− <listitem><para>
203− List of packages to be added into <literal>$PYTHONPATH</literal>.
204− Packages in <varname>pythonPath</varname> are not propagated
205− (contrary to <varname>propagatedBuildInputs</varname>).
206− </para></listitem>
207− </varlistentry>
208−
209− <varlistentry>
210− <term><varname>preShellHook</varname></term>
211− <listitem><para>
212− Hook to execute commands before <varname>shellHook</varname>.
213− </para></listitem>
214− </varlistentry>
215−
216− <varlistentry>
217− <term><varname>postShellHook</varname></term>
218− <listitem><para>
219− Hook to execute commands after <varname>shellHook</varname>.
220− </para></listitem>
221− </varlistentry>
222−
223− <varlistentry>
224− <term><varname>makeWrapperArgs</varname></term>
225− <listitem><para>
226− A list of strings. Arguments to be passed to
227− <varname>makeWrapper</varname>, which wraps generated binaries. By
228− default, the arguments to <varname>makeWrapper</varname> set
229− <varname>PATH</varname> and <varname>PYTHONPATH</varname> environment
230− variables before calling the binary. Additional arguments here can
231− allow a developer to set environment variables which will be
232− available when the binary is run. For example,
233− <varname>makeWrapperArgs = ["--set FOO BAR" "--set BAZ QUX"]</varname>.
234− </para></listitem>
235− </varlistentry>
236−
237− </variablelist>
238−
239−</section>
240−
241−<section xml:id="ssec-python-build-env"><title><function>python.buildEnv</function> function</title>
242− <para>
243− Create Python environments using low-level <function>pkgs.buildEnv</function> function. Example <filename>default.nix</filename>:
244−
245−<programlisting language="nix">
246−<![CDATA[with import <nixpkgs> {};
247−
248−python.buildEnv.override {
249− extraLibs = [ pkgs.pythonPackages.pyramid ];
250− ignoreCollisions = true;
251−}]]>
252−</programlisting>
253−
254− Running <command>nix-build</command> will create
255− <filename>/nix/store/cf1xhjwzmdki7fasgr4kz6di72ykicl5-python-2.7.8-env</filename>
256− with wrapped binaries in <filename>bin/</filename>.
257− </para>
258−
259− <para>
260− You can also use <varname>env</varname> attribute to create local
261− environments with needed packages installed (somewhat comparable to
262− <literal>virtualenv</literal>). For example, with the following
263− <filename>shell.nix</filename>:
264−
265−<programlisting language="nix">
266−<![CDATA[with import <nixpkgs> {};
267−
268−(python3.buildEnv.override {
269− extraLibs = with python3Packages;
270− [ numpy
271− requests
272− ];
273−}).env]]>
274−</programlisting>
275−
276− Running <command>nix-shell</command> will drop you into a shell where
277− <command>python</command> will have specified packages in its path.
278− </para>
279−
280− <variablelist>
281− <title>
282− <function>python.buildEnv</function> arguments
283− </title>
284−
285− <varlistentry>
286− <term><varname>extraLibs</varname></term>
287− <listitem><para>
288− List of packages installed inside the environment.
289− </para></listitem>
290− </varlistentry>
291−
292− <varlistentry>
293− <term><varname>postBuild</varname></term>
294− <listitem><para>
295− Shell command executed after the build of environment.
296− </para></listitem>
297− </varlistentry>
298−
299− <varlistentry>
300− <term><varname>ignoreCollisions</varname></term>
301− <listitem><para>
302− Ignore file collisions inside the environment (default is <varname>false</varname>).
303− </para></listitem>
304− </varlistentry>
305− </variablelist>
306−</section>
307−
308−<section xml:id="ssec-python-tools"><title>Tools</title>
309−
310−<para>Packages inside nixpkgs are written by hand. However many tools
311−exist in community to help save time. No tool is preferred at the moment.
312−</para>
313−
314−<itemizedlist>
315−
316− <listitem><para>
317− <link xlink:href="https://github.com/proger/python2nix">python2nix</link>
318− by Vladimir Kirillov
319− </para></listitem>
320−
321− <listitem><para>
322− <link xlink:href="https://github.com/garbas/pypi2nix">pypi2nix</link>
323− by Rok Garbas
324− </para></listitem>
325−
326− <listitem><para>
327− <link xlink:href="https://github.com/offlinehacker/pypi2nix">pypi2nix</link>
328− by Jaka Hudoklin
329− </para></listitem>
330−
331−</itemizedlist>
332−
333−</section>
334−
335−<section xml:id="ssec-python-development"><title>Development</title>
336−
337− <para>
338− To develop Python packages <function>buildPythonPackage</function> has
339− additional logic inside <varname>shellPhase</varname> to run
340− <command>pip install -e . --prefix $TMPDIR/</command> for the package.
341− </para>
342−
343− <warning><para><varname>shellPhase</varname> is executed only if <filename>setup.py</filename>
344− exists.</para></warning>
345−
346− <para>
347− Given a <filename>default.nix</filename>:
348−
349−<programlisting language="nix">
350−<![CDATA[with import <nixpkgs> {};
351−
352−buildPythonPackage {
353− name = "myproject";
354−
355− buildInputs = with pkgs.pythonPackages; [ pyramid ];
356−
357− src = ./.;
358−}]]>
359−</programlisting>
360−
361− Running <command>nix-shell</command> with no arguments should give you
362− the environment in which the package would be build with
363− <command>nix-build</command>.
364− </para>
365−
366− <para>
367− Shortcut to setup environments with C headers/libraries and python packages:
368−
369− <programlisting language="bash">$ nix-shell -p pythonPackages.pyramid zlib libjpeg git</programlisting>
370− </para>
371−
372− <note><para>
373− There is a boolean value <varname>lib.inNixShell</varname> set to
374− <varname>true</varname> if nix-shell is invoked.
375− </para></note>
376−
377−</section>
378−
379−<section xml:id="ssec-python-faq"><title>FAQ</title>
380−
381−<variablelist>
382−
383− <varlistentry>
384− <term>How to solve circular dependencies?</term>
385− <listitem><para>
386− If you have packages <varname>A</varname> and <varname>B</varname> that
387− depend on each other, when packaging <varname>B</varname> override package
388− <varname>A</varname> not to depend on <varname>B</varname> as input
389− (and also the other way around).
390− </para></listitem>
391− </varlistentry>
392−
393− <varlistentry>
394− <term><varname>install_data / data_files</varname> problems resulting into <literal>error: could not create '/nix/store/6l1bvljpy8gazlsw2aw9skwwp4pmvyxw-python-2.7.8/etc': Permission denied</literal></term>
395− <listitem><para>
396− <link xlink:href="https://bitbucket.org/pypa/setuptools/issue/130/install_data-doesnt-respect-prefix">
397− Known bug in setuptools <varname>install_data</varname> does not respect --prefix</link>. Example of
398− such package using the feature is <filename>pkgs/tools/X11/xpra/default.nix</filename>. As workaround
399− install it as an extra <varname>preInstall</varname> step:
400−
401− <programlisting>${python.interpreter} setup.py install_data --install-dir=$out --root=$out
402−sed -i '/ = data_files/d' setup.py</programlisting>
403− </para></listitem>
404− </varlistentry>
405−
406− <varlistentry>
407− <term>Rationale of non-existent global site-packages</term>
408− <listitem><para>
409− There is no need to have global site-packages in Nix. Each package has isolated
410− dependency tree and installing any python package will only populate <varname>$PATH</varname>
411− inside user environment. See <xref linkend="ssec-python-build-env" /> to create self-contained
412− interpreter with a set of packages.
413− </para></listitem>
414− </varlistentry>
415−
416−</variablelist>
417−
418−</section>
419−
420−
421−<section xml:id="ssec-python-contrib"><title>Contributing guidelines</title>
422−<para>
423− Following rules are desired to be respected:
424−</para>
425−
426−<itemizedlist>
427−
428− <listitem><para>
429− Make sure package builds for all python interpreters. Use <varname>disabled</varname> argument to
430− <function>buildPythonPackage</function> to set unsupported interpreters.
431− </para></listitem>
432−
433− <listitem><para>
434− If tests need to be disabled for a package, make sure you leave a comment about reasoning.
435− </para></listitem>
436−
437− <listitem><para>
438− Packages in <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/python-packages.nix"><filename>pkgs/top-level/python-packages.nix</filename></link>
439− are sorted quasi-alphabetically to avoid merge conflicts.
440− </para></listitem>
441−
442−</itemizedlist>
443−
444−</section>
445−
446−</section>
447−