Coverage.py running into ModuleNotFoundError errors

Hi, I have a set of pytest unit tests, one of the functions include Google Cloud client library. If I run the unit tests as $pytest, they run just fine, however, I run it as coverage run -m pytest, it fails with the following error:

$ coverage run -m pytest
ImportError while loading conftest '/Users/myuser/my_app/tests/conftest.py'.
tests/conftest.py:3: in <module>
    from app.main import app
app/main.py:2: in <module>
    from app.api import notification
app/api/notification.py:4: in <module>
    from app.api.k8s_util import K8sUtil
app/api/k8s_util.py:4: in <module>
    from google.cloud import container_v1
E   ModuleNotFoundError: No module named 'google.cloud'

@nedbat any ideas? I’m sure my configuration is missing something

thanks for your help

I might put import sys; print(sys.path) at the top of conftest.py, run both ways, and see if there is a difference that would explain.

quick update, $pytest command is not printing anything and $coverage run -m pytest is printing paths that do not reflect the python virtual environment from where I’m running the test suite:

[‘/Users/myuser/my_app’,
‘/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python310.zip’,
‘/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10’,
‘/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload’,
‘/opt/homebrew/lib/python3.10/site-packages’]

Instead of coverage run, try using python -m coverage run to ensure you are using the correct Python installation.

@nedbat wouldn’t that run just coverage but not pytest ? My apologies if my question doesn’t make much sense, I’m fairly new into Python.

When I run the suggested command, is not doing much:

$ python -m coverage run
Nothing to do.
Use 'coverage help' for help.
Full documentation is at https://coverage.readthedocs.io

Sorry, meant to replace part of the command. You’d need python -m coverage run -m pytest

ahhh, python -m coverage run -m pytest command worked fine, does it mean when I run $coverage run -m pytest is using a different python configuration?