Search code examples
pythonmatplotlibpython-3.6travis-cipyside2

Travis-CI failing to build due to PySide2 and Matplotlib


I have a github repository where I develop an electrical calculation software.

Recently I completely migrated from PyQt5 to PySide2.

Today I added Travis-CI for continuous integration as a hook to the Githb repository. This means that when I push some changes, travis-CI launches a build of my repository in an independent machine in their cloud.

Travis CI is failing due to this:

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

The failing file plot_config.py has the following matplotlib configuration at the beginning:

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

This code is what I've found on several threads to be the right thing to do. However the automated tests on an independent machine fail because of them.

So, What is the best and unambiguous way to tell matplotlib to use PySide2?

The packages required are (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

Solution

  • You must enable the XVFB service as indicated in the docs to be able to test libraries that need a graphical environment:

    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
    

    I have created a PR to your repository.