Merge pull request #58160 from worldofpeace/python-doc/cleanup

doc/python: cleanup examples, references
This commit is contained in:
worldofpeace 2019-04-08 12:43:22 -04:00 committed by GitHub
commit 82688a1525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -278,9 +278,9 @@ The following example shows which arguments are given to `buildPythonPackage` in
order to build [`datashape`](https://github.com/blaze/datashape). order to build [`datashape`](https://github.com/blaze/datashape).
```nix ```nix
{ # ... { lib, buildPythonPackage, fetchPypi, numpy, multipledispatch, dateutil, pytest }:
datashape = buildPythonPackage rec { buildPythonPackage rec {
pname = "datashape"; pname = "datashape";
version = "0.4.7"; version = "0.4.7";
@ -289,8 +289,8 @@ order to build [`datashape`](https://github.com/blaze/datashape).
sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278"; sha256 = "14b2ef766d4c9652ab813182e866f493475e65e558bed0822e38bf07bba1a278";
}; };
checkInputs = with self; [ pytest ]; checkInputs = [ pytest ];
propagatedBuildInputs = with self; [ numpy multipledispatch dateutil ]; propagatedBuildInputs = [ numpy multipledispatch dateutil ];
meta = with lib; { meta = with lib; {
homepage = https://github.com/ContinuumIO/datashape; homepage = https://github.com/ContinuumIO/datashape;
@ -298,12 +298,11 @@ order to build [`datashape`](https://github.com/blaze/datashape).
license = licenses.bsd2; license = licenses.bsd2;
maintainers = with maintainers; [ fridh ]; maintainers = with maintainers; [ fridh ];
}; };
};
} }
``` ```
We can see several runtime dependencies, `numpy`, `multipledispatch`, and We can see several runtime dependencies, `numpy`, `multipledispatch`, and
`dateutil`. Furthermore, we have one `buildInput`, i.e. `pytest`. `pytest` is a `dateutil`. Furthermore, we have one `checkInputs`, i.e. `pytest`. `pytest` is a
test runner and is only used during the `checkPhase` and is therefore not added test runner and is only used during the `checkPhase` and is therefore not added
to `propagatedBuildInputs`. to `propagatedBuildInputs`.
@ -313,9 +312,9 @@ Python bindings to `libxml2` and `libxslt`. These libraries are only required
when building the bindings and are therefore added as `buildInputs`. when building the bindings and are therefore added as `buildInputs`.
```nix ```nix
{ # ... { lib, pkgs, buildPythonPackage, fetchPypi }:
lxml = buildPythonPackage rec { buildPythonPackage rec {
pname = "lxml"; pname = "lxml";
version = "3.4.4"; version = "3.4.4";
@ -324,7 +323,7 @@ when building the bindings and are therefore added as `buildInputs`.
sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk"; sha256 = "16a0fa97hym9ysdk3rmqz32xdjqmy4w34ld3rm3jf5viqjx65lxk";
}; };
buildInputs = with self; [ pkgs.libxml2 pkgs.libxslt ]; buildInputs = [ pkgs.libxml2 pkgs.libxslt ];
meta = with lib; { meta = with lib; {
description = "Pythonic binding for the libxml2 and libxslt libraries"; description = "Pythonic binding for the libxml2 and libxslt libraries";
@ -332,7 +331,6 @@ when building the bindings and are therefore added as `buildInputs`.
license = licenses.bsd3; license = licenses.bsd3;
maintainers = with maintainers; [ sjourdois ]; maintainers = with maintainers; [ sjourdois ];
}; };
};
} }
``` ```
@ -347,9 +345,9 @@ find each of them in a different folder, and therefore we have to set `LDFLAGS`
and `CFLAGS`. and `CFLAGS`.
```nix ```nix
{ # ... { lib, pkgs, buildPythonPackage, fetchPypi, numpy, scipy }:
pyfftw = buildPythonPackage rec { buildPythonPackage rec {
pname = "pyFFTW"; pname = "pyFFTW";
version = "0.9.2"; version = "0.9.2";
@ -360,7 +358,7 @@ and `CFLAGS`.
buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble]; buildInputs = [ pkgs.fftw pkgs.fftwFloat pkgs.fftwLongDouble];
propagatedBuildInputs = with self; [ numpy scipy ]; propagatedBuildInputs = [ numpy scipy ];
# Tests cannot import pyfftw. pyfftw works fine though. # Tests cannot import pyfftw. pyfftw works fine though.
doCheck = false; doCheck = false;
@ -376,7 +374,6 @@ and `CFLAGS`.
license = with licenses; [ bsd2 bsd3 ]; license = with licenses; [ bsd2 bsd3 ];
maintainers = with maintainers; [ fridh ]; maintainers = with maintainers; [ fridh ];
}; };
};
} }
``` ```
Note also the line `doCheck = false;`, we explicitly disabled running the test-suite. Note also the line `doCheck = false;`, we explicitly disabled running the test-suite.
@ -403,7 +400,7 @@ Indeed, we can just add any package we like to have in our environment to `propa
```nix ```nix
with import <nixpkgs> {}; with import <nixpkgs> {};
with pkgs.python35Packages; with python35Packages;
buildPythonPackage rec { buildPythonPackage rec {
name = "mypackage"; name = "mypackage";
@ -436,7 +433,7 @@ Let's split the package definition from the environment definition.
We first create a function that builds `toolz` in `~/path/to/toolz/release.nix` We first create a function that builds `toolz` in `~/path/to/toolz/release.nix`
```nix ```nix
{ lib, pkgs, buildPythonPackage }: { lib, buildPythonPackage }:
buildPythonPackage rec { buildPythonPackage rec {
pname = "toolz"; pname = "toolz";
@ -456,18 +453,17 @@ buildPythonPackage rec {
} }
``` ```
It takes two arguments, `pkgs` and `buildPythonPackage`. It takes an argument `buildPythonPackage`.
We now call this function using `callPackage` in the definition of our environment We now call this function using `callPackage` in the definition of our environment
```nix ```nix
with import <nixpkgs> {}; with import <nixpkgs> {};
( let ( let
toolz = pkgs.callPackage /path/to/toolz/release.nix { toolz = callPackage /path/to/toolz/release.nix {
pkgs = pkgs; buildPythonPackage = python35Packages.buildPythonPackage;
buildPythonPackage = pkgs.python35Packages.buildPythonPackage;
}; };
in pkgs.python35.withPackages (ps: [ ps.numpy toolz ]) in python35.withPackages (ps: [ ps.numpy toolz ])
).env ).env
``` ```
@ -565,7 +561,7 @@ buildPythonPackage rec {
''; '';
checkInputs = [ hypothesis ]; checkInputs = [ hypothesis ];
buildInputs = [ setuptools_scm ]; nativeBuildInputs = [ setuptools_scm ];
propagatedBuildInputs = [ attrs py setuptools six pluggy ]; propagatedBuildInputs = [ attrs py setuptools six pluggy ];
meta = with lib; { meta = with lib; {
@ -585,11 +581,6 @@ The `buildPythonPackage` mainly does four things:
environment variable and add dependent libraries to script's `sys.path`. environment variable and add dependent libraries to script's `sys.path`.
* In the `installCheck` phase, `${python.interpreter} setup.py test` is ran. * In the `installCheck` phase, `${python.interpreter} setup.py test` is ran.
As in Perl, dependencies on other Python packages can be specified in the
`buildInputs` and `propagatedBuildInputs` attributes. If something is
exclusively a build-time dependency, use `buildInputs`; if it is (also) a runtime
dependency, use `propagatedBuildInputs`.
By default tests are run because `doCheck = true`. Test dependencies, like By default tests are run because `doCheck = true`. Test dependencies, like
e.g. the test runner, should be added to `checkInputs`. e.g. the test runner, should be added to `checkInputs`.
@ -733,7 +724,7 @@ Saving the following as `default.nix`
with import <nixpkgs> {}; with import <nixpkgs> {};
python.buildEnv.override { python.buildEnv.override {
extraLibs = [ pkgs.pythonPackages.pyramid ]; extraLibs = [ pythonPackages.pyramid ];
ignoreCollisions = true; ignoreCollisions = true;
} }
``` ```
@ -815,11 +806,12 @@ Given a `default.nix`:
```nix ```nix
with import <nixpkgs> {}; with import <nixpkgs> {};
buildPythonPackage { name = "myproject"; pythonPackages.buildPythonPackage {
name = "myproject";
buildInputs = with pythonPackages; [ pyramid ];
buildInputs = with pkgs.pythonPackages; [ pyramid ]; src = ./.;
}
src = ./.; }
``` ```
Running `nix-shell` with no arguments should give you Running `nix-shell` with no arguments should give you
@ -1005,10 +997,13 @@ Create this `default.nix` file, together with a `requirements.txt` and simply ex
```nix ```nix
with import <nixpkgs> {}; with import <nixpkgs> {};
with pkgs.python27Packages; with python27Packages;
stdenv.mkDerivation { stdenv.mkDerivation {
name = "impurePythonEnv"; name = "impurePythonEnv";
src = null;
buildInputs = [ buildInputs = [
# these packages are required for virtualenv and pip to work: # these packages are required for virtualenv and pip to work:
# #
@ -1028,8 +1023,9 @@ stdenv.mkDerivation {
libxslt libxslt
libzip libzip
stdenv stdenv
zlib ]; zlib
src = null; ];
shellHook = '' shellHook = ''
# set SOURCE_DATE_EPOCH so that we can use python wheels # set SOURCE_DATE_EPOCH so that we can use python wheels
SOURCE_DATE_EPOCH=$(date +%s) SOURCE_DATE_EPOCH=$(date +%s)