Python: Difference between revisions

DHCP (talk | contribs)
m Libraries: fix indentation
Bittner (talk | contribs)
Suggest HM's programs.uv option and nix-ld
 
(One intermediate revision by one other user not shown)
Line 7: Line 7:
Create a file <code>shell.nix</code> in the project directory, with the following template:
Create a file <code>shell.nix</code> in the project directory, with the following template:


{{file|shell.nix|lang="nix"|3=
{{file|shell.nix|nix|3=
let
let
   # We pin to a specific nixpkgs commit for reproducibility.
   # We pin to a specific nixpkgs commit for reproducibility.
Line 68: Line 68:


Generally, you may create a file that looks like this:
Generally, you may create a file that looks like this:
{{file|toolz.nix|lang="nix"|3=
{{file|toolz.nix|nix|3=
{
{
   lib,
   lib,
Line 98: Line 98:
}}
}}
Given the file above is named <code>toolz.nix</code> and is the same directory as the previous <code>shell.nix</code> , you can edit <code>shell.nix</code> to use the package <code>toolz</code> above like so:
Given the file above is named <code>toolz.nix</code> and is the same directory as the previous <code>shell.nix</code> , you can edit <code>shell.nix</code> to use the package <code>toolz</code> above like so:
{{file|shell.nix|lang="nix"|3=
{{file|shell.nix|nix|3=
let
let
   pkgs = import <nixpkgs> {};
   pkgs = import <nixpkgs> {};
Line 403: Line 403:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ uv python install 3.14 --preview --default
$ uv python install 3.14 --default
</syntaxhighlight>
</syntaxhighlight>


You may want to set the <code>UV_PYTHON_DOWNLOADS=never</code> environment variable in your shell to stop uv from downloading Python binaries automatically if needed. Setting <code>environment.localBinInPath = true;</code> is highly recommended, because uv will install binaries in <code>~/.local/bin</code>.
Setting <code>environment.localBinInPath = true;</code> is highly recommended, because uv will install binaries in <code>~/.local/bin</code>.
 
==== Python software development ====
Python is meant to be considered a ''development'' package. That said, avoid installing a ''system'' Python for developing on Python projects. Consider using Home Manager's [https://nix-community.github.io/home-manager/options/home-manager/programs/uv.html <code>programs.uv</code>] instead that does Python and Python tool installations with continuous, automatic updates.
 
==== ImportError: libstdc++.so.6 ====
If you hit the infamous <code>ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory</code> error after installing a Python package make sure to configure <code>nix-ld</code> (see above) to patch packages that expect an [[wikipedia:Filesystem_Hierarchy_Standard|FHS compliant]] environment. See [https://painless.software/how-use-python-on-nixos-the-pythonic-way this blog post] for background reading.


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