How to solve ImportError with pytest and python

Viewed 27

I've tried to use the solutions already provided but none of them seem to be working.

Here's my project structure-

.
├── ent-add-api-consumer
    ├── lambdas
    |      ├─clients
    |      |   ├─http_client.py
    |      |   ├── __init__.py
    |      └─createContingency
    |          ├─ADD_createContingency.py 
    |          └── __init__.py
    └── test
    |     ├─ unit\consumer
    |     |          ├─test_create_contingency_service.py 
    |     |          └── __init__.py
    |     ├── __init__.py
    |     └── conftest.py
    └── .env

Here's the starting part of the code to the test file(test_create_contingency_service.py)

from unittest.mock import MagicMock

from freezegun import freeze_time

from clients import http_client
from eis_util_lib_v2.model.http_request import HttpRequest, HttpResponse
from aws_lambda_powertools.utilities import parameters


@freeze_time("2022-06-30")
def test_create_contingency_workflow(
    consolidated_report_item, create_contingency_response
):

And I have created a PYTHONPATH environment variable under my system variable: PYTHONPATH="lambdas:test/unit/" system vars

I was getting a warning in my file to the import statement from clients import http_client

So vscode created a settings.json with below code when I clicked on the fix:

{
    "python.envFile": "${workspaceFolder}/.env",
    "python.analysis.extraPaths": [
        "./lambdas"
    ]
}

(The first line in setting.json is what I added later to debug the error)

Here's my .env file:

PYTHONPATH=${PYTHONPATH}:./lambdas:./test/unit:./test:/venv/bin/python
LOG_LEVEL=INFO
appName=ent-sls-add-api

When I try running the test script through -> python test\unit\consumer\test_create_contingency_service.py. I get the below error:

    D:\Users\SAH5252\.aws\Work\ActiveDriverDiscoveryPython\ent-add-api-consumer>python test\unit\consumer\test_create_contingency_service.py
Traceback (most recent call last):
  File "D:\Users\SAH5252\.aws\Work\ActiveDriverDiscoveryPython\ent-add-api-consumer\test\unit\consumer\test_create_contingency_service.py", line 5, in <module>
    from clients import http_client
ModuleNotFoundError: No module named 'clients'

Error while running the test scripts

And when I try to run pytest through this command python -m pytest test/unit/ in the root folder, I get the below error:

D:\Users\SAH5252\.aws\Work\ActiveDriverDiscoveryPython\ent-add-api-consumer>python -m pytest test/unit/
===================================================================================== test session starts =====================================================================================
platform win32 -- Python 3.10.7, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\Users\SAH5252\.aws\Work\ActiveDriverDiscoveryPython\ent-add-api-consumer
plugins: tavern-1.16.5
collected 0 items / 1 error

=========================================================================================== ERRORS ============================================================================================
___________________________________________________________ ERROR collecting test/unit/consumer/test_create_contingency_service.py ____________________________________________________________ 
ImportError while importing test module 'D:\Users\SAH5252\.aws\Work\ActiveDriverDiscoveryPython\ent-add-api-consumer\test\unit\consumer\test_create_contingency_service.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Program Files\Python310\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test\unit\consumer\test_create_contingency_service.py:5: in <module>
    from clients import http_client
E   ModuleNotFoundError: No module named 'clients'
=================================================================================== short test summary info =================================================================================== 
ERROR test/unit/consumer/test_create_contingency_service.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
====================================================================================== 1 error in 0.61s ======================================================================================= 

pytest error

I've tried giving absolute path to lambdas folder in the PYTHONPATH Variable but I get the same error. Could this be an issue with PYTHONPATH, that my python interpreter doesn't know the path it needs to look into to find the imported files. I'm not sure what I'm doing wrong or if any other configuration is need to run this.

Any help is appreciated thank you.

0 Answers
Related