Python: Difference between revisions

imported>Georgeoshardo
m Updated micromamba's availability
imported>DavHau
No edit summary
Line 255: Line 255:


=== mach-nix ===
=== mach-nix ===
Mach-nix is a tool that allows managing python environments with nix based on a `requirements.txt` file.
There are two different ways of how mach-nix can be used, either by installing the mach-nix cmdline tool or by writing a nix expression. The latter is recommended for maximum reproducibility.


Install the package
An example for a nix expression using mach-nix to build a python environment defined by a list of requirements.
<code>nix-env -if https://github.com/DavHau/mach-nix/tarball/3.1.1 -A mach-nix</code>
<syntaxhighlight lang="nix">
or
<code>pip install git+git://github.com/DavHau/mach-nix@3.1.1</code>
and run
<syntaxhighlight lang="shell">
mach-nix env ./env -r requirements.txt
# This will generate the python environment into ./env. To activate it, execute:
nix-shell ./env
</syntaxhighlight>
or
<syntaxhighlight lang="shell">
# nix.extraOptions = ''  experimental-features = nix-command flakes    '';
nix-shell -p nixFlakes --run "nix run github:davhau/mach-nix#with.ipython.geopandas --show-trace "
</syntaxhighlight>
 
cmd-line tool:
<syntaxhighlight lang="shell">
let
let
   mach-nix = import (builtins.fetchGit {
   mach-nix = import (builtins.fetchGit {
     url = "https://github.com/DavHau/mach-nix/";
     url = "https://github.com/DavHau/mach-nix/";
     ref = "refs/tags/3.1.1";
    # place version number with the latest one from the github releases page
     ref = "refs/tags/3.4.0";
   }) {};
   }) {};
in
in
mach-nix.mkPython {
mach-nix.mkPython {
  # contents of a requirements.txt (use builtins.readFile ./requirements.txt alternatively)
   requirements = ''
   requirements = ''
     pillow
     pillow
Line 288: Line 276:
}
}
</syntaxhighlight>
</syntaxhighlight>
Alternatively install the mach-nix cmdline tool
<code>nix-env -if https://github.com/DavHau/mach-nix/tarball/3.4.0 -A mach-nix</code>
and run
<syntaxhighlight lang="shell">
mach-nix env ./env -r requirements.txt
# This will generate the python environment into ./env. To activate it, execute:
nix-shell ./env
</syntaxhighlight>
Or use a single command based on nix' flakes feature.
<syntaxhighlight lang="shell">
# nix.extraOptions = ''  experimental-features = nix-command flakes    '';
nix-shell -p nixFlakes --run "nix run github:davhau/mach-nix#with.ipython.geopandas --show-trace "
</syntaxhighlight>
Please have a look at more [https://github.com/DavHau/mach-nix/blob/master/examples.md examples].
Please have a look at more [https://github.com/DavHau/mach-nix/blob/master/examples.md examples].