ImportError: No module named _backend_gdk

13,053

Solution 1

Note that the Debian/Ubuntu package you need is not 'pygtk2-devel' but 'python-gtk2-dev':

sudo apt-get install python-gtk2-dev

should fix this error on these platforms.

Solution 2

This was a symptom of using a pip-installed matplotlib instead of an apt-installed matplotlib on my system, just now. If on Ubuntu/Debian, try:

pip uninstall matplotlib
apt install python-matplotlib

I believe what was happening is that the pip-install didn't build the C extension required for GTK output, but the apt package has the extension prebuilt. I don't have logs from the initial pip install of matplotlib, so I can't confirm that that's what happened.

Solution 3

In addition to Haldean Brown's answer, note that if you really need to use pip you can force it to recompile matplotlib locally and get "the deep magic that setup.py does" with the --no-binary option:

pip uninstall matplotlib
pip install matplotlib --no-binary=matplotlib

This will solve your problem, provided that you already installed gtk2 with sudo apt-get install python-gtk2-dev

As you want to use the GTKAgg backend, Using pip may prove useful in the future to freeze matplotlib at a version where it is supported (the deprecation warning states it will be dropped in 3.0):

pip install matplotlib==2.2.2 --no-binary=matplotlib

Solution 4

Ubuntu 18.04 has a broken matplotlib package. See this bug: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458

You can either compile matplotlib yourself (e.g. with pip) or use the PPA linked from the bug report.

Share:
13,053
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I am starting to get some insight into interactive plotting with python and matplotlib using pyGTK+. Therefore I took a look at the example given at the matplotlib website.

    This is a short exerpt of the Code:

    #!/usr/bin/env python
    """
    Example of embedding matplotlib in an application and interacting with
    a treeview to store data.  Double click on an entry to update plot
    data
    
    """
    import pygtk
    pygtk.require('2.0')
    import gtk
    from gtk import gdk
    
    import matplotlib
    matplotlib.use('GTKAgg')  # or 'GTK'
    from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
    
    from numpy.random import random
    from matplotlib.figure import Figure
    

    Ones I try to run this Script in the Terminal I get the following error:

    Traceback (most recent call last):
      File "gtk_spreadsheet.py", line 15, in <module>
        from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 33, in <module>
        from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
        from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
    ImportError: No module named _backend_gdk
    

    I have python 2.7 and pygtk 2.24 installed.

    Can anyone figure out where the error is located? I think it might be connected to some linking issues?

  • Admin
    Admin about 10 years
    This answer should be accepted. +1 for clarity, correctness and conciseness.
  • Davide
    Davide about 9 years
    This answer is irrelevant with the problem at hand
  • 2LayerNN300HU
    2LayerNN300HU about 7 years
    This is just annoying, why pip doesn't work? I think having to install the package from apt prevents us to use any version other than the one supported but the distro. Still I found you answer the only working solution.
  • jdpipe
    jdpipe over 6 years
    Yes, this answer doesn't help. I have installed standard python-matplotlib packages, and I can't plot using my usual GTK backends, even though the _backend_gdk file is there in my system.
  • spirit
    spirit over 5 years
    On Ubuntu 18.04, this does not solve the problem. I still get the same error message. There is a bug for this here: bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458