Python: Difference between revisions

Dyego (talk | contribs)
m add Using nix-shell alongside pip section
Samuela (talk | contribs)
Line 1: Line 1:


== Python environments with Nix ==
== Python development environments with Nix ==
Python environment allow you to run Python code with the <code>python</code> command. It may also have Python packages installed as well.
Nix supports a number of approaches to creating "development environments" for Python programming. These provide functionality analogous to [https://virtualenv.pypa.io/en/latest/ virtualenv] or [https://docs.conda.io/en/latest/ conda]: a shell environment with access to pinned versions of the <code>python</code> executable and Python packages.


Nix allows to create many independent Python environment, much like <code>venv</code>.
=== Using the Nixpkgs Python infrastructure via <code>shell.nix</code> (recommended) ===
 
There are many ways to create a Python environment with Nix.
 
=== Using regular Python virtual environment ===
 
It is possible to use Nix to create a Python virtual environment for your project. You can then use the Python virtual environment as usual.
 
To create a Python virtual environment:<syntaxhighlight lang="console">
$ nix-shell -p python3 --command "python -m venv .venv --copies"
</syntaxhighlight>You can then activate and use the Python virtual environment as usual and install dependencies with <code>pip</code> and similar.
 
==== On NixOS ====
This method may not work on NixOS, as installing packages with <code>pip</code> that need to compile code or use C libraries will fail due to not finding dependencies in the expected places.
 
There are multiple ways to make it work:
 
* Use [https://github.com/GuillaumeDesforges/fix-python/ fix-python], this is most suited for beginners.
 
* Create a FHS user env with <code>buildFHSUserEnv</code>.
* Setup <code>nix-ld</code> in your NixOS configuration.
 
=== Using the Nixpkgs Python infrastructure ===
Nixpkgs has the few last Python versions packaged, as well as a consequent set of Python packages packaged that you can use to quickly create a Python environment.
Nixpkgs has the few last Python versions packaged, as well as a consequent set of Python packages packaged that you can use to quickly create a Python environment.


Line 45: Line 23:
</syntaxhighlight>
</syntaxhighlight>


This example will make Nix create a Python environment with Python packages "pandas" and "requests". You can remove them, or add more.
In this example, we create a Python environment with packages <code>pandas</code> and <code>requests</code>.
 
You can find Python packages that are available in Nixpkgs using [https://search.nixos.org/packages search.nixos.org]. For instance, type a Python package name (for example <code>numpy</code>) in the search bar and click on the search button on the right. Then on the left in the "Package sets" section click on "python311Packages" to list only Python packages. for Python (version 3.11).


Note that in the snippet above, on line 8 and 9, each package to install is in the form <code>python-pkgs.<name></code>. The package name must correspond to the one found in [https://search.nixos.org/packages search.nixos.org] as explained above. To understand why it is prefixed by <code>python-pkgs</code>, read the [https://nix.dev/tutorials/nix-language.html Nix language basics].
You can find Python packages that are available in Nixpkgs using [https://search.nixos.org/packages search.nixos.org]. For instance, type a Python package name like <code>numpy</code> in the search bar and click on the search button on the right. You can narrow down results by clicking on eg. "python311Packages" in the "Package sets" section on the left. Note that in the snippet above, on lines 8 and 9, each package is listed in the form <code>python-pkgs.<name></code> where <code><name></code> corresponds to the one found in [https://search.nixos.org/packages search.nixos.org] . See [https://nix.dev/tutorials/nix-language.html Nix language basics] for more information on the <code>python-pkgs</code> attribute set.


Then, once you have picked the Python packages you want, run <code>nix-shell</code> (or <code>nix develop -f shell.nix</code>) to make Nix create the Python environment and enter it. If it succeeds, Python should be available in your PATH, try using <code>python --version</code> at this point.
Once you have picked the Python packages you want, run <code>nix-shell</code> (or <code>nix develop -f shell.nix</code>) to build the Python environment and enter it. Once in the environment Python will be available in your PATH, so you can run eg. <code>python --version</code>.


As a word of warning, using <code>pkgs = import <nixpkgs> {};</code> as in the snippet above (line 3) will result in a non-reproducible environment. Use Nixpkgs pinning or Nix Flakes to make your environment reproducible.
As a word of warning, using <code>pkgs = import <nixpkgs> {};</code> as in the snippet above (line 3) will result in a non-reproducible environment. Use Nixpkgs pinning or Nix Flakes to make your environment reproducible.


==== Using a Python packages not in Nixpkgs ====
==== Using a Python packages not in Nixpkgs ====
Python packages in Nixpkgs are created and updated by Nixpkgs maintainers. Although the community invests a great effort to keep a complete and up-to-date package set, it isn't uncommon for packages you want to be missing, out of date or broken. To use your own packages in a Nix environment, you may package it yourself.
Python packages in Nixpkgs are created and updated by Nixpkgs maintainers. Although the community invests a great effort to keep a complete and up-to-date package set, some packages you want may be missing, out of date, or broken. To use your own packages in a Nix environment, you may package it yourself.


The following is a high-level overview. For a complete explanation, see [https://nixos.org/manual/nixpkgs/unstable/#developing-with-python Developing with Python] in the Nixpkgs Manual.
The following is a high-level overview. For a complete explanation, see [https://nixos.org/manual/nixpkgs/unstable/#developing-with-python Developing with Python] in the Nixpkgs Manual.
Line 107: Line 83:


Next time you enter the shell specified by this file, Nix will build and include the Python package you have written.
Next time you enter the shell specified by this file, Nix will build and include the Python package you have written.
==== Using nix-shell alongside pip  ====
==== Using nix-shell alongside pip  ====
When working on a collaborative python project you may want to be able to <code>pip install -r requirements.txt</code> if the project isn't packaged for nix specifically.
When working on a collaborative python project you may want to be able to <code>pip install -r requirements.txt</code> if the project isn't packaged for nix specifically.
Line 210: Line 184:
shell
shell
</syntaxhighlight>
</syntaxhighlight>
=== Using <code>venv</code> ===
To create a Python virtual environment with <code>venv</code>:<syntaxhighlight lang="console">
$ nix-shell -p python3 --command "python -m venv .venv --copies"
</syntaxhighlight>You can then activate and use the Python virtual environment as usual and install dependencies with <code>pip</code> and similar.
==== On NixOS ====
This method may not work on NixOS, as installing packages with <code>pip</code> that need to compile code or use C libraries will fail due to not finding dependencies in the expected places.
There are multiple ways to make it work:
* Use [https://github.com/GuillaumeDesforges/fix-python/ fix-python], this is most suited for beginners.
* Create a FHS user env with <code>buildFHSUserEnv</code>.
* Setup <code>nix-ld</code> in your NixOS configuration.


=== Using poetry ===
=== Using poetry ===