Search code examples
pythontravis-ci

I'm getting the Travis CI Build Error "Module not found" and I'm not sure why


I'm getting "Module not found" errors for my Travis CI build. I'm not sure exactly why but here's a look at my initial PyTest file:

import requests

url_ddg = "https://api.duckduckgo.com"


def test_ddg0():
    resp = requests.get(url_ddg + "/?q=DuckDuckGo&format=json")
    rsp_data = resp.json()
    assert "DuckDuckGo" in rsp_data["Heading"]

My travis.yml file contains this following code:

language: python
python:
 - "3.8.2"
 - "nightly"
install:
 - "pip install pytest"
 - "pip install -r requirements.txt"
script: python -m pytest Test.py

Here's a little look at the error on Travis build log:

==================================== ERRORS ====================================
___________________________ ERROR collecting Test.py ___________________________
ImportError while importing test module '/home/travis/build/mjfields-java/PresidentsAPI/Test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
Test.py:1: in <module>
    import requests
E   ModuleNotFoundError: No module named 'requests'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.07 seconds ============================
The command "python -m pytest Test.py" exited with 2.
Done. Your build exited with 1.

My requirements.txt file contains this:

pytest==4.6

Solution

  • Is the requests module part of your requirements.txt file? If not, that's the problem. requests is sometimes installed by default, but usually not in Linux distributions like what Travis usually runs on.

    requirements.txt can look like:

    pytest==4.6
    requests~=2.6.0