将PySide2设置为matplotlib后端的正确方法

时间:2019-06-30 17:16:10

标签: python matplotlib python-3.6 travis-ci pyside2

我有一个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

1 个答案:

答案 0 :(得分:3)

您必须按照docs中的指示启用XVFB服务,以便能够测试需要图形环境的库:

<ion-datetime 
              [(ngModel)]="start"
              displayFormat="HH:mm" 
              minuteValues="0,15,30,45" 
              picker-format="HH:mm"
              (ionChange)="onChangeStartTime($event)"
</ion-datetime>

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