Python: Difference between revisions
imported>Krey |
imported>Krey No edit summary |
||
| Line 1: | Line 1: | ||
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. | ||
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"> | ||
with pkgs; | |||
let | |||
my-python-packages = python-packages: with python-packages; [ | |||
pandas | pandas | ||
requests | requests | ||
# other python packages you want | # other python packages you want | ||
]; | ]; | ||
python-with-my-packages = python3.withPackages my-python-packages | |||
in ... | |||
</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> | 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. | ||
=== | == 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> | |||
==== Imperative use ==== | |||
<syntaxhighlight lang=" | It is also possible to use <code>conda-install</code> directly. | ||
On first use, run | |||
<syntaxhighlight lang="shell"> | |||
conda-shell | |||
conda-install | |||
</syntaxhighlight> | </syntaxhighlight> | ||
to set up conda in <code>~/.conda</code> | |||
=== pypi2nix === | |||
== Contribution guidelines == | == Contribution guidelines == | ||