Get PYTHONPATH from pkgs.python3.withPackages

Hello,

I have noticed that rycee’s home-manager does not set PYTHONPATH when adding pkgs.python3.withPackages to the program list while nix-shell does it properly.

How can I get the PYTHONPATH such as the one built by nix-shell in order to set it as an environment variable manually in my home.nix ?

For instance if I evaluate:

let pkgs = import <nixpkgs> {}; in pkgs.python3.withPackages (pp: with pp; [ pytorchWithCuda ])

is there an attribute to that set that can help me retrieve the python env librairies?

I could then build the PYTHONPATH with lib.makeSearchPath.

From

I can guess that it should be held in an extraLibs attribute, but using nix repl I can’t seem to find it.

Thanks

1 Like

you can pass a list of Python packages to e.g. python3.pkgs.makePythonPath if you need a PYTHONPATH, but you should avoid using PYTHONPATH.

This is intentional. In general, you don’t want to packages from one environment to bleed over to another.

if you just want PYTHONPATH to be set, you could just use the raw python packages themselves, instead of:

pkgs.python3.withPackages (pp: with pp; [ pytorchWithCuda ])

do

with pkgs.python3.pkgs; [ pytorchWithCuda ]

the shellhook on the python packages will execute in the shell, and this will add them to PYTHONPATH

you should avoid using PYTHONPATH .

I know, but it’s a request from one of my users.
For more context, NixOS is the only OS on which I could successfully install NVIDIA, CUDA and PyTorch with CUDA support on my server (tried Ubuntu and Manjaro). Yet another testimony in favor of it haha.

with pkgs.python3.pkgs; [ pytorchWithCuda ]

the shellhook on the python packages will execute in the shell

I’ve tried it but it seems that the shell hook is not executed on login if it’s set up from home-manager.

I ended up doing something like this:

{ config, pkgs, ... }:

let
  python-with-packages = pkgs.python3.withPackages (pp: with pp; [
    pytorchWithCuda
    jupyter
  ]);

in
  {
    programs.home-manager.enable = true;
    home.stateVersion = "19.09";

    home.packages = [
      python-with-packages
    ];

    home.sessionVariables = {
      PYTHONPATH = "${python-with-packages}/${python-with-packages.sitePackages}";
    };
  }

So then the user can create a venv

nix-shell --pure -p python3Packages.virtualenv --run "python -m venv .venv"

and have PyTorch with CUDA

$ source .venv/bin/activate
(.venv)
$ python -c "import torch; print(torch.cuda.is_available())"
True
3 Likes