Python: Difference between revisions

imported>Krey
imported>Krey
No edit summary
Line 1: Line 1:
== 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.


Supposing you want Python 3 with <code>pandas</code> and <code>requests</code>, then <code>configuration.nix</code> should then look something like
To install, say Python 3 with <code>pandas</code> and <code>requests</code>, define a new package <code>python-with-my-packages</code>:
 
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
environment.systemPackages = let  
with pkgs;
   myPythonPackages = pythonPackages: with pythonPackages; [
let
   my-python-packages = python-packages: with python-packages; [
     pandas
     pandas
     requests
     requests
     # other python packages you want
     # other python packages you want
   ];  
   ];  
in with pkgs; [
  python-with-my-packages = python3.withPackages my-python-packages
  (python3.withPackages myPythonPackages)
in ...
  # all your other (non-Python) packages
]
</syntaxhighlight>
</syntaxhighlight>
You can put <code>python-with-my-packages</code> into your environment.systemPackages for a system-wide installation, for instance.


There are several versions of Python available. Replace <code>python3</code> with <code>python2</code> or <code>pypy</code> in the above snippet according to your needs.
There are several versions of Python available. Replace <code>python3</code> with <code>python2</code> or <code>pypy</code> in the above snippet according to your needs.
Line 22: Line 20:
=== Explanation (optional) ===
=== Explanation (optional) ===


We defined a function <code>myPythonPackages</code> which takes as input a set <code>pythonPackages</code> and returns a list of attributes thereof.
We defined a function <code>my-python-packages</code> which takes as input a set <code>python-packages</code> and returns a list of attributes thereof.


=== Dealing with missing/outdated packages ===
== Using alternative packages ==
 
We saw above how to install Python packages using nixpkgs. Since these are written by hand by nixpkgs maintainers, it isn't uncommon for packages you want to be missing or out of date.
 
=== pip ===
 
Add <code>virtualenvwrapper</code> to your list of Python packages above. Pure Python packages can be installed in a virtualenv using pip. The package <code>pandas</code> in the example below won't work.
 
Put your packages in a requirements.txt:
<syntaxhighlight>
pandas
requests
</syntaxhighlight>
 
Then setup the virtualenv:
<syntaxhighlight lang="shell">
virtualenv my-new-python-venv
source my-new-python-venv/bin/activate
pip install -r requirements.txt
</syntaxhighlight>
 
=== conda ===
 
Install the package <code>conda</code> and run
<syntaxhighlight lang="shell">
conda-shell
conda-env create --name my-new-conda-env -f requirements.txt
</syntaxhighlight>


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


<syntaxhighlight lang="console">
It is also possible to use <code>conda-install</code> directly.
$ virtualenv my-new-python-venv
On first use, run
$ source my-new-python-venv/bin/activate
<syntaxhighlight lang="shell">
$ pip install pandas_datareader # or some other package you want
conda-shell
conda-install
</syntaxhighlight>
</syntaxhighlight>
to set up conda in <code>~/.conda</code>


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


== Contribution guidelines ==
== Contribution guidelines ==