ipython: QXcbConnection: Could not connect to display

When I create a completely fresh conda envirnoment:

conda create --name test ipython matplotlib

and then try to import matplotlib.pyplot from within ipython, I get the following:

In [1]: import matplotlib.pyplot

In [2]: QXcbConnection: Could not connect to display
Aborted

There is no problem when I run the same in the vanilla python shell. Happens for both py2.7.13 (ipython 5.3.0) and py3.6.1 (ipython 6.0.0).

I don’t know whether this is an ipython bug or a conda bug, so I raised an issue first with anaconda, but I wanted to note it here too.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 15
  • Comments: 32 (14 by maintainers)

Most upvoted comments

There’s a solution using environment variables available here:

Setting export QT_QPA_PLATFORM='offscreen' in my .bash_profile worked for me.

@russelljjarvis it’s a minor annoyance, but if the first command you run when you start up ipython is import matplotlib; matplotlib.use('agg') then ipython should work for you.

solution in notebook directly

import os
os.environ['QT_QPA_PLATFORM']='offscreen'

That’s really cool! Then I’d say this can be closed.

The OP can simply set ‘agg’ as Matplotlib’s default backend in his matplotlibrc to avoid this error.

The following setting does work!!!

export QT_QPA_PLATFORM=‘offscreen’

As a late-ish follow up, as of Matplotlib 3.0 we now support automatic backend selection and will not try to use a GUI backend on a headless server (but will still respect an rcparams file that asks us to)

Matplotlib 3.1 cannot support automatic backend, we need to manually set ‘Agg’ .

I am getting the same error:

Setting export QT_QPA_PLATFORM='offscreen' changed the error message to:

(python:17399): Gtk-WARNING **: cannot open display:

A colleague recently suggested the same thing. In the context of Docker your statement would be: ENV QT_QPA_PLATFORM offscreen. I am effectively testing that now.

I think moving to tk as default would be less than great.

What do you mean exactly? Do you think it’s better for Anaconda to stay with Qt5?

And if I explicitly set the backend to 'agg' I also don’t get the error, but I’d like to avoid having to do that every time I launch ipython and want to import something that imports matplotlib.

After setting QT_QPA_PLATFORM='offscreen' the imports of matplotlib and fastai libraries run. However, plt.figure() or any other command outputting a plot raises the following errors:

This plugin does not support propagateSizeHints()
This plugin does not support raise()

This is my error after setting export QT_QPA_PLATFORM=offscreen

[tb571@da02 ~]$ jupyter console --kernel slicer-4.11
qt.qpa.plugin: Could not find the Qt platform plugin "offscreen" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

This happens because the default backend for Matplotlib in Anaconda is Qt5. We’re considering to change it to Tk to avoid this kind of problems.