khteh
khteh

Reputation: 3956

pytest -v ModuleNotFoundError

I have the following structure:

|- src/
     |- common/
     |- controllers/
     |- models/
     |- __init__.py
|- test/
     |- __init__.py
     |- some_test.py

I am using python 3.12.7 and pipenv. Running pytest -v from both the root of my project or test/ folder always complains about something missing. For example:

$ pytest -v
==================================================================================== test session starts =====================================================================================
platform linux -- Python 3.12.7, pytest-8.3.2, pluggy-1.5.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /usr/src/Python/PythonRestAPI
plugins: typeguard-4.3.0
collected 75 items / 1 error                                                                                                                                                                 

=========================================================================================== ERRORS ===========================================================================================
________________________________________________________________________ ERROR collecting test/authentication_test.py ________________________________________________________________________
ImportError while importing test module '/usr/src/Python/PythonRestAPI/test/authentication_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/authentication_test.py:5: in <module>
    from common.Authentication import Authentication
src/common/Authentication.py:5: in <module>
    from models.UserModel import UserModel
src/models/__init__.py:7: in <module>
    from .AuthorModel import AuthorModel, AuthorSchema
src/models/AuthorModel.py:1: in <module>
    from marshmallow import fields, Schema
E   ModuleNotFoundError: No module named 'marshmallow'
================================================================================== short test summary info ===================================================================================
ERROR test/authentication_test.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================== 1 error in 0.34s ======================================================================================
$ pipenv graph|grep -i marshmallow
marshmallow==3.26.1

conftest.py:

import pytest
import os, sys
from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '../src')))

@pytest.fixture
def client():
    from app import create_app
    return create_app().test_client()

def pytest_generate_tests(metafunc):
        os.environ['JWT_SECRET_KEY'] = 'pythonflaskrestapipostgres'

pipenv shell:

$ which python
/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/bin/python
$ which pytest
/home/khteh/.local/bin/pytest

Upvotes: 0

Views: 19

Answers (0)

Related Questions