简体   繁体   中英

pytest failing in AWS SAM project due to ModuleNotFoundError

I have an AWS SAM project with the following folder structure:

account/
   __init__.py
   layers/
       __init__.py
       config/
           settings.py
       utils/
           logger.py
   get_accounts/
       app.py
       requirements.txt
   tests/
        requirements.txt
        integration/
            test_api_gateway.py
        unit/
            test_handler.py

When I run python3 -m pytest tests/unit/ from the project root directory, I get the following error:

ImportError while importing test module '/Users/johnsmith/dev/myproject/account/tests/unit/test_handler.py'. Hint: make sure your test modules/packages have valid Python names.

Traceback: /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/ init .py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests/unit/test_handler.py:3: in from get_accounts import app get_accounts/app.py:14: in from layers.utils.logger import get_logger layers/utils/logger.py:1: in from config.settings import ApplicationConfig E ModuleNotFoundError: No module named 'config'

The issue is when the test file imports the main application, main application has dependencies that lie in "layers" folder; For some reason, it's not able to import those files. When I start the application using sam local command, it works, but it fails when I run pytest.

I've tried

  • adding __init__.py to all folders under "layers" folder.
  • Creating a setup.py and specifying "config" as the package name, then running pip install -e . based on pytest's documentation
  • In get_accounts/app.py , specifying "layers" before config.settings and utils.logger when importing those items
  • Removing __init__.py from test folder

These were all possible solutions mentioned from stackoverflow, but none of them worked for me.

Here's how the imports in get_accounts/app.py look like:

import json
import requests
from config.settings import ApplicationConfig
from utils.logger import get_logger
from pprint import pprint

Does anyone have any ideas on what I'm doing wrong and how I can get it working?

I managed to reproduce more or less what you are encountering. Here are my files:

account > cat tests/unit/test_import.py 
import get_accounts.app

def test_hello():
    print("Hello World")
account > cat get_accounts/app.py 
from config.settings import ApplicationConfig

account > cat layers/config/settings.py 

class ApplicationConfig:
    pass

It raises an error saying config cannot be imported. Changing from:

from config.settings import ApplicationConfig

to

from layers.config.settings import ApplicationConfig

Solves the problem in my setup.

account > python3 -m pytest tests/
===================================================================== test session starts ======================================================================platform linux -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/brunno/account
collected 1 item

tests/unit/test_import.py .                                                                                                                              [100%] 

====================================================================== 1 passed in 0.01s =======================================================================

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM