Description
Been having a ton of trouble running the scripts/test-local.sh
as instructed in the README
. I keep running into ModulenotFoundError
issues in backend/app/app/tests/conftest.py
I've confirmed the following are working:
- My backend Docker container is copying over
backend/app
to/app
as expected. /app
is getting set as the Python path as expected.- Imports work normally when
bash
'd into the backend container. - Imports work normally when running the backend service.
sys.path
contains/app
as expected (I print it out while runningpytest -s
).
However, no matter how I seem to run pytest
(bash
'd into the backend service, docker compose run backend pytest
, or the test-local.sh
script), I get the following error:
==================================================================================================== ERRORS ====================================================================================================
________________________________________________________________________________________ ERROR collecting test session _________________________________________________________________________________________
/usr/local/lib/python3.10/site-packages/_pytest/config/__init__.py:641: in _importconftest
mod = import_path(conftestpath, mode=importmode, root=rootpath)
/usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:567: in import_path
importlib.import_module(module_name)
/usr/local/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
???
<frozen importlib._bootstrap>:1027: in _find_and_load
???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:688: in _load_unlocked
???
/usr/local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
exec(co, module.__dict__)
app/tests/conftest.py:14: in <module>
from app.core.config import settings
E ModuleNotFoundError: No module named 'app.core'
When I try to append an extra app.
to the imports (e.g., from app.app.core.config import settings
, the error goes away, but then my regular backend service crashes (ModuleNotFoundError app.app
). Clearly it is some difference between how pytest and running the service regularly handle imports.
My file structure is the same as the template project: the main.py
for FastAPI lives at backend/app/app
, my pyproject.toml
lives at backend/app
.
I am running pytest 7.1.3
, python 3.10
, and Mac OS 14.1.