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__.pyto 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" beforeconfig.settingsandutils.loggerwhen importing those items - Removing
__init__.pyfrom 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?