tox testenv已添加pandas到deps仍报ModuleNotFoundError如何解决
问题根因
从运行日志的警告信息可以直接定位问题:你调用的pytest是系统全局安装的版本(路径为/usr/local/bin/py.test),而非tox创建的py36_tests虚拟环境内安装的pytest。全局pytest运行时只会读取系统路径下的依赖包,自然找不到虚拟环境里安装的pandas。
此前添加boto3到deps后问题消失属于巧合:你的系统环境恰好也安装了boto3,所以全局pytest能找到对应包,并不代表配置是正确的。
修复方案
你只需要修改tox.ini中[testenv:py36_tests]的commands配置,将py.test改为python -m pytest即可:
[testenv:py36_tests] basepython=python3.6 deps=boto3 pandas commands=python -m pytest -s -v tests --junitxml=report.xml
该写法会强制使用当前虚拟环境的Python解释器加载pytest,保证能读取到同环境下安装的所有依赖包。
如果要进一步验证问题,可以临时在commands头部添加两行命令,执行后就能直观看到差异:
commands= which py.test pip list | grep pandas python -m pytest -s -v tests --junitxml=report.xml
内容的提问来源于stack exchange,提问作者AME
相关产品推荐
相关产品推荐

