brainstormtrooper
brainstormtrooper

Reputation: 495

In a Gitlab pipeline, if a python module is installed, why do I get a ModuleNotFoundError when I try to import it?

I am trying to run a python script as part of a Gitlab pipeline. It seems to start out ok and I can interact with python to get the version and have checked the system paths. However, when I pip(3) install something from my gitlab-ci.yml I cannot then import it in the python script I call in the next line.

For example:

test:
 script:
  - pip3 install mysql <-- this gives no errors and reports success
  - python3 -c "import mysql" <-- this fails with ModuleNotFoundError...

The import statement also fails in my runtest.py script if I try to execute that from the pipeline with

 - python3 runtest.py

Here is my current gitlab-ci file:

stages:
 - test

image: python:latest

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python3 -V
  - pip3 install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
 script:
  - pip3 install mysql
  - python3 -c "import mysql"
  - python3 runtest.py
 when: manual

What am I missing?

Any pointers are greatly appreciated. Thanks.

Upvotes: 1

Views: 1614

Answers (1)

VonC
VonC

Reputation: 1324606

Installing mysql itself is not enough.

Python would need mysql-connector as well, as seen in this answer.

pip3 install mysql-connector-python

Then see if the same ModuleNotFoundError still pops up.

Upvotes: 1

Related Questions