Python触发ImportError/ModuleNotFoundError:pytest跨目录导入模块失败
pytest跨目录模块导入异常排查与解决方案
异常复现场景
按照操作步骤,会先后触发两类报错:
- 当工作目录设置为
~/Downloads/principles_of_programming/object-oriented-programming/fibonacci时,执行命令pytest tests/test_fibonacci.py,pytest输出如下:
==================================================== test session starts ===================================================== platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 rootdir: C:\Users\Ryan\Downloads\principles_of_programming\object-oriented-programming, configfile: setup.cfg collected 0 items =================================================== no tests ran in 0.05s ==================================================== ERROR: file or directory not found: tests/test_fibonacci.py
- 当工作目录切换到
~/Downloads/principles_of_programming/object-oriented-programming后,执行完全相同的pytest命令,pytest输出如下:
==================================================== test session starts ===================================================== platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 rootdir: C:\Users\Ryan\Downloads\principles_of_programming\object-oriented-programming, configfile: setup.cfg collected 0 items / 1 error =========================================================== ERRORS =========================================================== __________________________________________ ERROR collecting tests/test_fibonacci.py __________________________________________ ImportError while importing test module 'C:\Users\Ryan\Downloads\principles_of_programming\object-oriented-programming\tests\test_fibonacci.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: ..\..\..\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests\test_fibonacci.py:1: in <module> from fibonacci import fib E ModuleNotFoundError: No module named 'fibonacci' ================================================== short test summary info =================================================== ERROR tests/test_fibonacci.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ====================================================== 1 error in 0.45s ======================================================
问题根因定位
两类报错的触发逻辑完全独立,分别对应不同的配置问题:
- 找不到测试文件的报错:pytest查找传入的测试文件路径时,是基于当前工作目录做相对路径解析的。当工作目录在
fibonacci子文件夹下时,目标路径tests/test_fibonacci.py在当前目录层级下不存在(tests目录实际位于上一级的object-oriented-programming文件夹中),因此直接抛出文件不存在的错误。 - 模块导入失败的报错:当工作目录切换到object-oriented-programming目录后,pytest成功定位到测试文件的路径,但Python的模块搜索路径默认仅包含当前工作目录、系统Python安装路径、已安装第三方包路径,并未将
fibonacci子目录纳入可搜索范围,因此执行测试文件中from fibonacci import fib的导入语句时,会抛出ModuleNotFoundError。
修复方案
可根据实际开发需求选择对应方案,不需要调整原有测试文件和业务代码的位置:
- 方案一:使用pytest内置机制自动修正路径
在项目根目录(object-oriented-programming文件夹下)新建空的conftest.py文件即可。pytest在启动收集用例时,会自动将conftest.py所在的目录加入Python的模块搜索路径,此时fibonacci作为同层级的子目录模块,可被正常识别导入。 - 方案二:按标准Python包布局调整项目结构
适合长期维护的项目场景:- 在项目根目录下新建
src文件夹,将原有fibonacci文件夹移入src目录 - 在项目根目录添加
pyproject.toml配置文件,声明项目的可安装元数据 - 执行
pip install -e .完成本地可编辑安装,安装完成后任意工作目录下执行pytest,都能正常导入fibonacci模块
- 在项目根目录下新建
- 方案三:临时调整运行时路径(仅适合快速调试)
不需要修改项目文件,运行pytest前手动将当前项目根目录加入Python路径环境变量:- Windows环境执行:
set PYTHONPATH=%cd% && pytest tests/test_fibonacci.py - macOS/Linux环境执行:
export PYTHONPATH=$(pwd) && pytest tests/test_fibonacci.py
- Windows环境执行:
内容的提问来源于stack exchange,提问作者Christopher Nickolson
相关产品推荐
相关产品推荐

