From 9c898df3d59f1493c540adb28d7b4bbf0eb0aa16 2018-09-10 13:44:06 From: Matthias Bussonnier Date: 2018-09-10 13:44:06 Subject: [PATCH] Backport PR #10974: IPython breaks import matplotlib.pyplot when there is no display --- diff --git a/IPython/terminal/pt_inputhooks/qt.py b/IPython/terminal/pt_inputhooks/qt.py index 0de9c3d..7395ac3 100644 --- a/IPython/terminal/pt_inputhooks/qt.py +++ b/IPython/terminal/pt_inputhooks/qt.py @@ -1,15 +1,29 @@ import sys +import os from IPython.external.qt_for_kernel import QtCore, QtGui # If we create a QApplication, keep a reference to it so that it doesn't get # garbage collected. _appref = None - +_already_warned = False def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() if not app: + if sys.platform == 'linux': + if not os.environ.get('DISPLAY') \ + and not os.environ.get('WAYLAND_DISPLAY'): + import warnings + global _already_warned + if not _already_warned: + _already_warned = True + warnings.warn( + 'The DISPLAY or WAYLAND_DISPLAY enviroment variable is ' + 'not set or empty and Qt5 requires this enviroment ' + 'variable. Deactivate Qt5 code.' + ) + return _appref = app = QtGui.QApplication([" "]) event_loop = QtCore.QEventLoop(app)