ImportError: No module named _backend_gdk

醉酒当歌 提交于 2019-11-29 03:51:55
Mark McWiggins

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.

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!