How to import numpy inside a jenkins build?

I have this working python program:

$ cat /mnt/apollinaire/test.py 
#!/usr/bin/env python3
import numpy

print("Hello world.")

which is working well on the command-line:

$ /mnt/apollinaire/test.py 
Hello world.

I have a jenkins build with an “execute shell” step defined as follows;

/mnt/apollinaire/test.py

When I run the build from jenkins it fails with the following error in the console:

Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/test numpy
[test numpy] $ /bin/sh -xe /tmp/jenkins11100534516892808761.sh
+ /mnt/apollinaire/test.py
Traceback (most recent call last):
  File "/mnt/apollinaire/test.py", line 2, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'
Build step 'Execute shell' marked build as failure
Finished: FAILURE

My NixOS configuration looks like this:

  environment.systemPackages = with pkgs;
    let my-python-packages = ps: with ps; [
      ...
      numpy
      ...
    ];
    in [
    (python3.withPackages my-python-packages)
    ...
    python310
    ...
  ];


  services.jenkins.packages = with pkgs; [
    bash
    python310
    python310Packages.numpy
    toybox
    ...
  ];

I don’t know how to make the numpy packages importable to my python script running inside the jenkins build step. Any help is appreciated.

Solved it with

 services.jenkins.packages = with pkgs;
    let my-python-packages = ps: with ps; [ numpy ]; in [
    (pkgs.python3.withPackages my-python-packages)
    bash
    python310
    toybox
    ...
  ];
1 Like