使用pipenv运行pytest -v遭遇ModuleNotFoundError问题求助
Pipenv环境下运行pytest提示依赖缺失问题
项目结构
|- src/ |- common/ |- controllers/ |- models/ |- __init__.py |- test/ |- __init__.py |- some_test.py
问题现象
使用Python 3.12.7和Pipenv,无论在项目根目录还是test文件夹下执行pytest -v,都会提示依赖缺失。例如执行pytest -v时出现以下错误:
$ pytest -v ==================================================================================== test session starts ===================================================================================== platform linux -- Python 3.12.7, pytest-8.3.2, pluggy-1.5.0 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /usr/src/Python/PythonRestAPI plugins: typeguard-4.3.0 collected 75 items / 1 error =========================================================================================== ERRORS =========================================================================================== ________________________________________________________________________ ERROR collecting test/authentication_test.py ________________________________________________________________________ ImportError while importing test module '/usr/src/Python/PythonRestAPI/test/authentication_test.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /usr/lib/python3.12/importlib/__init__.py:90: in import_module return _bootstrap._gcd_import(name[level:], package, level) test/authentication_test.py:5: in <module> from common.Authentication import Authentication src/common/Authentication.py:5: in <module> from models.UserModel import UserModel src/models/__init__.py:7: in <module> from .AuthorModel import AuthorModel, AuthorSchema src/models/AuthorModel.py:1: in <module> from marshmallow import fields, Schema E ModuleNotFoundError: No module named 'marshmallow' ================================================================================== short test summary info =================================================================================== ERROR test/authentication_test.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ====================================================================================== 1 error in 0.34s ======================================================================================
依赖检查结果
执行pipenv graph|grep -i marshmallow确认marshmallow已安装:
$ pipenv graph|grep -i marshmallow marshmallow==3.26.1
conftest.py内容
import pytest import os, sys from os.path import dirname, join, abspath sys.path.insert(0, abspath(join(dirname(__file__), '../src'))) @pytest.fixture def client(): from app import create_app return create_app().test_client() def pytest_generate_tests(metafunc): os.environ['JWT_SECRET_KEY'] = 'pythonflaskrestapipostgres'
Pipenv环境路径信息
在pipenv shell中执行以下命令,得到Python和pytest的路径:
$ which python /home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/bin/python $ which pytest /home/khteh/.local/bin/pytest
内容的提问来源于stack exchange,提问作者khteh
相关产品推荐
相关产品推荐

