Python: Difference between revisions

From NixOS Wiki
imported>Mic92
No edit summary
imported>Mic92
No edit summary
Line 1: Line 1:
== Installation ==
== System-wide installation ==


The Python packages available to the interpreter must be declared when installing Python.
The Python packages available to the interpreter must be declared when installing Python.
Line 6: Line 6:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
environment.systemPackages =
environment.systemPackages = let
let myPythonPackages = pythonPackages: with pythonPackages; [
  myPythonPackages = pythonPackages: with pythonPackages; [
  pandas
    pandas
  requests
    requests
  # other python packages you want
    # other python packages you want
];
  ];  
in
in with pkgs; [
with pkgs; [
   (python3.withPackages myPythonPackages)
   (python3.withPackages myPythonPackages)
   # all your other (non-Python) packages
   # all your other (non-Python) packages

Revision as of 22:14, 10 March 2018

System-wide installation

The Python packages available to the interpreter must be declared when installing Python.

Supposing you want Python 3 with pandas and requests, then configuration.nix should then look something like

environment.systemPackages = let 
  myPythonPackages = pythonPackages: with pythonPackages; [
    pandas
    requests
    # other python packages you want
  ]; 
in with pkgs; [
  (python3.withPackages myPythonPackages)
  # all your other (non-Python) packages
]

There are several versions of Python available. Replace python3 with python2 or pypy in the above snippet according to your needs.

Explanation (optional)

We defined a function myPythonPackages which takes as input a set pythonPackages and returns a list of attributes thereof.

Dealing with missing/outdated packages

Add virtualenvwrapper to your list of Python packages above. You can use pip safely inside such a virtualenv, most pure Python packages should work.

$ virtualenv my-new-python-venv
$ source my-new-python-venv/bin/activate
$ pip install pandas_datareader # or some other package you want

This, of course, goes against the declarative nature of the rest of the configuration and should be avoided if possible.

Contribution guidelines

Libraries

According to the official guidelines for python new package expressions for libraries should be placed in pkgs/development/python-modules/<name>/default.nix. Those expressions are then referenced from pkgs/top-level/python-packages.nix like in this example:

{
  aenum = callPackage ../development/python-modules/aenum { };
}

The reasoning behind this is the large size of pkgs/top-level/python-packages.nix. Unfortunately most libraries are still defined in-place in pkgs/top-level/python-packages.nix. If a change to library is necessary or an update is made, it is recommend to move the modified package out of pkgs/top-level/python-packages.nix.

Applications

Python applications instead should be referenced directly from pkgs/top-level/all-packages.nix.

The expression should take pythonPackages as one of the arguments, which guarantees that packages belong to the same set. For example:

{ lib
, pythonPackages
}:

with pythonPackages;

buildPythonApplication rec {
# ...

External Documentation