Search code examples
pythonpipgitlab-cicicdmodulenotfounderror

How to resolve ModuleError when I already installed the module in the virtual environment, in gitlab yml?


I have a main.pyx file, which I cythonize, to give main.c. I build this C file in gitlab CI/CD, and run it, but it gives me NoModuleFoundError.

~

main.pyx

import numpy as np
..

~

cython main.pyx --embed

~

.gitlab-ci.yml

stages:

- build

image: python:3.9.16

variables:
HTTP_PROXY: $CODE_PROXY
HTTPS_PROXY: $CODE_PROXY

before_script:

- apt-get clean
- apt-get --allow-unauthenticated update
- apt-get upgrade
- apt-get install -y gcc
- apt-get update
- apt search '^python\[0-9\]\*-dev'
- python3 --version

build-job:  
stage: build
script:
- echo "Compiling the code..."
- python3 -m pip install --upgrade pip
- python3 -m venv venv
- source /path/venv/bin/activate
- python3 -m pip install --upgrade pip
- pip install pyimagej
- pip show numpy
- python3 check_py.py
- gcc -v main.c -I /usr/include/python3.9/  -I /usr/include/ -I /usr/local/include -I /usr/local/include/python3.9 -L /usr/lib/python3.9 -lpython3.9 -o main_output
- ./main_output
- echo "Compile complete."

~

I tried running the CI/CD, and I get the following error:

Traceback (most recent call last):
  File "main.pyx", line 1, in init main
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

I am expecting main_output to run smoothly, but I am unsure how to make this work. Any comments on this matter would be appreciated.


Solution

  • I had to just direct the pyx file to the right location for numpy package.

    import sys
    sys.path.append(".../venv/lib/python3.9/site-packages")
    import numpy as np
    

    Quite simple!