'Python on Gitlab has ModuleNotFound, But Not When I Run Locally

The following snapshot shows the file structure: enter image description here

When I run on Gitlab CI, here is what I am seeing:

Gitlab output

Why is this error occurring when Gitlab runs it but not when I run locally?

Here is my .gitlab-ci.yml file.

Note that this had been working before.
I recently made win_perf_counters a Git submodule instead of being an actual subdirectory. (Again, it works locally.)

test:
  before_script:
    - python -V
    - pip install virtualenv
    - virtualenv venv
    - .\venv\Scripts\activate.ps1
    - refreshenv
  script:
  - python -V
  - echo "******* installing pip ***********"
  - python -m pip install --upgrade pip
  - echo "******* installing locust ********"
  - python -m pip install locust
  - locust -V
  - python -m pip install multipledispatch
  - python -m pip install pycryptodome
  - python -m pip install pandas
  - python -m pip install wmi
  - python -m pip install pywin32
  - python -m pip install influxdb_client
  - set LOAD_TEST_CONF=load_test.conf
  - echo "**** about to run locust ******"
  - locust -f ./src/main.py --host $TARGET_HOST -u $USERS -t $TEST_DURATION -r $RAMPUP -s 1800 --headless --csv=./LoadTestsData_VPOS --csv-full-history --html=./LoadTestsReport_VPOS.html  --stream-file ./data/stream_jsons/streams_vpos.json --database=csv
  
    
  - Start-Sleep -s $SLEEP_TIME

  variables:
    LOAD_TEST_CONF: load_test.conf
    PYTHON_VERSION: 3.8.0
    TARGET_HOST: http://10.10.10.184:9000

  tags:
    - win2019
  artifacts:
    paths:
      - ./LoadTests*
      - public
  only:
    - schedules

  after_script:
    - ls src -r
    - mkdir .public
    - cp -r ./LoadTests* .public
    - cp metrics.csv .public -ErrorAction SilentlyContinue
    - mv .public public

When I tried with changing the Gitlab CI file to use requirements.txt: enter image description here



Solution 1:[1]

Probably the python libraries you are using in your local environment are not the same you are using in gitlab. Run a pip list or pip freeze in your local machine and see which versions do you have there. Then pip install those in your gitlab script. A good practice is to have a requirements.txt or a setup.py file with specific versions rather than pulling the latest versions every time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matias Thayer