Travis-CI 由于 PySide2 和 Matplotlib 而无法构建

Travis-CI failing to build due to PySide2 and Matplotlib

我有一个 github 存储库,我在其中开发了 electrical calculation software.

最近我完全从 PyQt5 迁移到 PySide2。

今天我添加了 Travis-CI 用于持续集成作为 Githb 存储库的挂钩。这意味着当我推送一些更改时,travis-CI 在他们云中的独立机器中启动我的存储库的构建。

Travis CI 因以下原因而失败:

ImportError while importing test module '/home/travis/build/SanPen/GridCal/src/tests/test_branch_tolerance.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: src/tests/test_branch_tolerance.py:1: in <module>
    from GridCal.Engine import * src/GridCal/Engine/__init__.py:17: in <module>
    from GridCal.Engine.basic_structures import * src/GridCal/Engine/basic_structures.py:23: in <module>
    from GridCal.Engine.plot_config import LINEWIDTH, plt src/GridCal/Engine/plot_config.py:18: in <module>
    matplotlib.use('Qt5Agg') ../../../virtualenv/python3.6.7/lib/python3.6/site-packages/matplotlib/cbook/deprecation.py:307: in wrapper
    return func(*args, **kwargs) ../../../virtualenv/python3.6.7/lib/python3.6/site-packages/matplotlib/__init__.py:1297: in use
    switch_backend(name) ../../../virtualenv/python3.6.7/lib/python3.6/site-packages/matplotlib/pyplot.py:230: in switch_backend
    newbackend, required_framework, current_framework)) 

E   ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework, as 'headless' is currently running

失败文件plot_config.py开头有如下matplotlib配置:

import PySide2  # this line is necessary so that Matplotlib recognises that PySide is the Qt Backend
import matplotlib
matplotlib.use('Qt5Agg')
# matplotlib.rcParams['backend.qt5'] = 'PySide2'  # this is not supported anymore
from matplotlib import pyplot as plt

这段代码是我在多个线程中发现的正确做法。然而,独立机器上的自动化测试却因为它们而失败。

那么,告诉 matplotlib 使用 PySide2 的最佳且明确的方法是什么?

需要的包是(requirements.txt):

PySide2>=5.11 
numpy>=1.14.0 
scipy>=1.0.0 
networkx>=2.1 
pandas>=0.22 
xlwt>=1.3.0 
xlrd>=1.1.0 
matplotlib>=3.1.0 
qtconsole>=4.3.1 
pyDOE>=0.3.8 
pySOT>=0.2.1 
openpyxl>=2.4.9 
pulp>=1.6.8 
smopy>=0.0.6 
chardet>=3.0.4 
scikit-learn>=0.18 
geopy>=1.16 
pytest>=3.8 
h5py>=2.9.0

您必须启用 docs 中指示的 XVFB 服务才能测试需要图形环境的库:

dist: xenial   # required for Python >= 3.7
services:
  - xvfb
language: python
python:
  - "3.6"
  - "3.7"
# command to install dependencies
install:
  - pip install -r requirements.txt
# command to run tests
script:
  - pytest

我已经为您的存储库创建了一个 PR