Я прошел через многочисленные ответы и попробовал большинство из них, но все еще сталкивался с той же проблемой
Моя структура
-- backend
-- app
-- __init__.py
-- utils.py
-- models.py
-- pytest
-- test_utils.py`
Теперь я хочу импортировать утилиты в test_utils, чтобы иметь возможность вызывать функцию и тестировать ее. Мой init .py не пуст, и мой test_utils.py выглядит следующим образом
import sys, os.path
sys.path.insert(0 ,(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/app/'))
from utils import test_function
def test_test_function():
a = test_function(5)
assert a == 5
Я проверил, что мой путь sys также указывает на правильный каталог, но продолжаю получать эту ошибку, и я использую Python 2.7 в Linux
ImportError while importing test module '/home/richie/Documents/Project/backend/pytest/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../virenv/local/lib/python2.7/site-packages/six.py:709: in exec_
exec("""exec _code_ in _globs_, _locs_""")
test_utils.py:3: in <module>
from utils import test_function
../app/utils.py:15: in <module>
from models import Users, Accounts
../app/models.py:2: in <module>
from app import db
E ImportError: No module named app