How to install the python package "requests" system wide?

So I am trying to setup the python package “requests” so that it can be used from python on NixOS. I found various “snippets” for the systemPackages variable online, but all of them just cause convoluted error messages.

Some people say you should just use a venv or a nix shell, but I really do not want to launch something like that everytime I want to run one of my scripts, especially because I launch some of them via hotkey.

Basically, I am looking for the NixOS equivalent of “apt install python3-requests”.

See https://nixos.org/manual/nixos/stable/#sec-declarative-package-mgmt , only replace their example of pkgs.thunderbird with python314Packages.requests.

Thanks, but I tried that - it does not make requests available to python, i.e. “import requests” fails.

ModuleNotFoundError: No module named ‘requests’

1 Like

For THAT, unfortunately, the way NixOS works makes it complicated. You have various options. See

I had looked at that article before, but it just suggests

(python3.withPackages (python-pkgs: with python-pkgs; [
      requests
  ]))

which does not work, either.

This worked for me. What error message does it give for you?

The same:

import requests
Traceback (most recent call last):
File “”, line 1, in
import requests
ModuleNotFoundError: No module named ‘requests’

No error message from nixos-rebuild switch.

Hmm! What happens if you setup a quick dev shell with this command?

nix-shell -p "python3.withPackages (pypkgs: [ pypkgs.requests ])"

Yes, that works. But I find it cumbersome to have a shell like that for every script, especially since different scripts use different packages.

Yep, that was just a quick sanity check!

I think you’ll need to share (at least some of) your config, like where you are trying to put the above snippet.

My configuration.nix is over 200 lines already, but I believe this is the relevant part:

  environment.systemPackages = with pkgs; [
...

    # programming tools
    git
    go
    python3
    (python3.withPackages (python-pkgs: with python-pkgs; [
      requests
    ]))
...
];

I wonder if the “bare” python3 is interfering with the other?

  environment.systemPackages = with pkgs; [
...

    # programming tools
    git
    go
    #python3
    (python3.withPackages (python-pkgs: with python-pkgs; [
      requests
    ]))
...
];
1 Like

Yes, that was it! Thank you very much!

1 Like

Already solved, I saw, but in case your scripts are not ad-hoc but (possibly) part of your system or user config, an arguably better way would be to make them into python apps (as derivations) and just include the scripts as packages. That way they’ll always bring their “batteries included” regardless of global config, and they’ll still share common dependencies (same hash in the nix store).

2 Likes