Python: Difference between revisions
m add Using nix-shell alongside pip section |
|||
Line 1: | Line 1: | ||
== Python environments with Nix == | == Python development environments with Nix == | ||
Python environment | 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. | ||
=== Using the Nixpkgs Python infrastructure via <code>shell.nix</code> (recommended) === | |||
=== Using | |||
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> | ||
In this example, we create a Python environment with packages <code>pandas</code> and <code>requests</code>. | |||
Note that in the snippet above, on | 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. | ||
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, | 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 === |