You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub上tox测试运行报错ModuleNotFoundError:缺失matplotlib模块

问题描述

在GitHub Actions运行测试时触发如下报错,相同配置此前运行正常:

Run python -m tox -- --junit-xml pytest.xml
py38: install_deps> python -I -m pip install pytest pytest-benchmark pytest-xdist
tox: py38
  py38: commands[0]> pytest tests/ --ignore=tests/lab_extension --junit-xml pytest.xml
  ImportError while loading conftest '/home/runner/work/quibbler/quibbler/tests/conftest.py'.
  tests/conftest.py:6: in <module>
      from matplotlib import pyplot as plt
  E   ModuleNotFoundError: No module named 'matplotlib'
  py38: exit 4 (0.87 seconds) /home/runner/work/quibbler/quibbler> pytest tests/ --ignore=tests/lab_extension --junit-xml pytest.xml pid=1849
  py38: FAIL code 4 (4.01=setup[3.14]+cmd[0.87] seconds)
  evaluation failed :( (4.10 seconds)
Error: Process completed with exit code 4.

疑问:

  • tox近期是否有更新?
  • 该如何解决此问题?

解决方案

1. 补全缺失的测试依赖

从日志可见,tox在安装测试依赖时仅安装了pytest、pytest-benchmark、pytest-xdist,但测试代码的conftest.py依赖matplotlib,需将其加入依赖列表:

  • 若使用tox.ini配置,找到[testenv]下的deps字段,添加matplotlib;
  • 若通过测试依赖文件(如requirements-test.txt)管理,确保文件包含matplotlib,并在tox.ini的deps中配置-r requirements-test.txt来读取该文件。

2. 排查tox版本变更的影响

tox会定期发布新版本,若GitHub Actions环境自动升级了tox,可能引发依赖安装逻辑变化:

  • 检查工作流文件(如.github/workflows/xxx.yml),确认是否指定了tox固定版本,若未指定,环境可能拉取了最新版;
  • 临时指定此前运行正常的tox版本(如pip install tox==3.28.0)验证是否为版本问题;
  • 查阅tox官方更新日志,确认新版本是否有依赖安装相关的行为变更,例如是否不再自动继承全局依赖、或调整了deps配置的解析规则。

3. 额外验证动作

  • 在本地使用相同版本的tox运行测试,复现问题后调试;
  • 检查conftest.py是否近期新增了matplotlib导入,但依赖配置未同步更新。

内容的提问来源于stack exchange,提问作者royk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:15:09