Travis CI运行Python pytest构建时提示Module not found报错求助
报错原因
核心原因是项目的依赖声明不完整:
- 测试代码中导入了第三方库
requests,但你的requirements.txt文件仅声明了pytest依赖,未添加requests的依赖声明 - Travis CI的构建环境是全新的干净环境,不会默认预装
requests这类非标准库的第三方包,因此执行测试导入requests时就会触发ModuleNotFoundError
额外的冗余问题:你的travis.yml中install段单独执行了pip install pytest,但requirements.txt已经包含pytest依赖,该语句属于冗余配置,不会触发报错但可以优化。
解决方法
- 修改
requirements.txt文件,添加requests依赖声明,修改后内容示例如下:
pytest==4.6 requests>=2.25.1
你可以根据自己的实际需求调整requests的版本号。
- (可选优化)删除
travis.yml中install段冗余的pip install pytest语句,修改后的install配置如下:
install: - "pip install -r requirements.txt"
- 提交修改后的文件重新触发Travis CI构建即可正常运行测试。
内容的提问来源于stack exchange,提问作者Michael fields
相关产品推荐
相关产品推荐

