ImportError: No module named _backend_gdk - python

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?

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

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.

if you are running py2.7 applications, try:
pip2 install matplotlib --no-binary=matplotlib

Related

Pyplot not installing with matplotlib

I know that this is quite a common problem but when I installed this package I can't seem to get pyplot to be imported with matplotlib. My computer is currently running Pop os and I am using Pycharm as my IDE. When I run anything involved pyplot I get an the console prints out
Traceback (most recent call last):
File "/home/msm/PycharmProjects/Beginner/testing.py", line 1, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
I have already tried the following
sudo apt-get install python3-matplotlib in the terminal
Searching for the package within Pycharm
Updating Python
Any help would be greatly appreciated.
try:
pip install matplotlib
if it isn't working try:
pip install --upgrade pip
and try it again
if it isn't working again try start terminal with administrator and add python -m to head of code

ModuleNotFoundError: No module named 'websocket' even though I installed pip install websocket

I am trying to run some code that has 'import websocket' however I am getting the error: ModuleNotFoundError: No module named 'websocket'
I have Python 3.7.3 and I am running in Spyder (if that makes a difference).
So from other questions/answers I found on here, in my cmd I ran pip install websocket and then also pip install websocket-client when the first one didn't run.
I am still getting the ModuleNotFoundError. Does it matter the location/folder of the code or where I install the pip command in cmd?
My python code starts with these import statements:
import json
import websocket
import traceback
import helper
import ssl
import time as time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
In cmd I ran:
C:\Users\myname>pip install websocket
and also:
C:\Users\myname>pip install websocket-client
The error I am getting is:
File "C:/Users/micki/Downloads/Derbit-Volatility-Visulization-master/Derbit-Volatility-Visulization-master/Volatility Surface Class.py", line 2, in <module>
import websocket
ModuleNotFoundError: No module named 'websocket'
Not sure, as you did not cover how you installed and are using Spyder, though I think it is probably an issue with your environment. You might also find that you are missing the module "helper" as well. There's two easy options as follows:
If you installed and are using Spyder via conda or anaconda, follow their documentation on installing websocket-client to the correct environment found here.
The second option (the preferred option IMHO, as you can use any IDE or text editor going forward), regardless of how you installed Spyder, would be to create a python virtual environment python3 -m venv /path/to/new/virtual/environment, pip install all your dependencies in said environment, then link Spyder's interpreter to the interpereter that was installed when you made the environment. In Spyder, go to Tools -> Preferences -> Python interpreter -> check the "Use the following Python interpreter:" radio button and enter the path to the interpreter from the environment you just created. For reference, see docs on making and using a python venv here.
If the websocket and websocket-client doesn't work, try:
pip install websocket_client
This solved my issue:
sudo pip install websocket-client

Problems with Pyplot

Whenever I use Pyplot
import matplotlib.pyplot as plt
I got this problem. It shouldn't be installed the right way
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/artist.py", line 12, in <module>
from .transforms import Bbox, IdentityTransform, TransformedBbox, \
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/transforms.py", line 39, in <module>
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/_path.so, 2): Symbol not found: ___sincos_stret
Referenced from: /Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/_path.so
Expected in: /usr/lib/libSystem.B.dylib
in /Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/_path.so
any ideas on how to solve this problem.
Are you using MAC OSX 10.9? If not, it may be that you've installed the wrong version for your matplotlib. In my case, ScipySuperpack installed the wrong version for me although I followed precisely the instruction for Mac OSX 10.8.
If this is your problem, you may solve it through the steps below:
Download the older version of SpicySuperpack in zip format (in my case, through https://github.com/fonnesbeck/ScipySuperpack/archive/10.8_final.zip#).
Uninstall the old matplolib by typing: pip uninstall matplotlib.
Unzip the downloaded ScipySuperpack, if it has not been unzipped yet.
Go to the ScipySuperpack folder and type: easy_install matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg (use a different file name if this is not the one included in your downloaded ScipySuperpack folder)
Note: If you are using virtualenv, just activate your virtual environment before doing the easy_install.
Hope it helps!
If pip uninstall/install doesn't work, you might even have to go deeper. Do you get the same error with other modules within matplotlib or other modules in general from Python?
My suggestion, as you are on OSX, is to use homebrew to manage the installation of Python and pip and then everything should work much smoother.
Easiest is
pip uninstall matplotlib
pip install matplotlib
You may even have to go further to do the same with numpy.
There is some important information regarding getting matplotlib running on OS-X here most significantly:
"Which python for OS X?
Apple ships with its own python, and many users have had trouble with it. There are several alternative versions of python that can be used. If it is feasible, we recommend that you use the enthought python distribution EPD for OS X (which comes with matplotlib and much more). Also available is MacPython or the official OS X version from python.org.
"

Scipy, Numpy, Matplotlib Troubles on OSX

I just installed scipy and numpy using homebrew and pip. I did the following:
brew install python
brew install gfortran
easy_install pip
sudo pip install numpy
sudo pip install scipy
numpy and scipy are both easily found in /Library/Python/2.7/site-packages, but when I open a python shell in the terminal and type:
import numpy
import scipy
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
and the same for scipy. What's wrong? Pip seems to think they're installed.
The python shell you are running from the terminal is probably OSX pre-bundled python and not the one you installed (with numpy & scipy).
You can list the current paths using:
import sys
print(sys.path)
It might be a bit overkill, but I once wrote a short guide to install Python 2.7.x in OS X. You can find it here.
The bottom line is that right now you might have two versions of Python installed, and it can be a bit tricky to uninstall only one of them. If you're sure which one is using the right site-packages folder, then delete the other and update your path variables. If not, I'd suggest to follow that guide.

Unable to locate freeimage after install of mahotas in Python

Hi I am new to Python and am following the Python Image Tutorial.
The following executes with no errors after installing the packages described in the tutorial
import numpy
import scipy
import pylab
import pymorph
import mahotas
from scipy import ndimage
However when I try reading an image
image = mahotas.imread('picture_file.jpg')
I get
image = mahotas.imread('image_file.jpg')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.6/dist-packages/mahotas-0.6.4-py2.6-linux-i686.egg/mahotas/init.py", line 68, in imread
raise ImportError('mahotas.imread dependends on freeimage. Could not find it. Error was: %s' % e)
ImportError: mahotas.imread dependends on freeimage. Could not find it. Error was: mahotas.freeimage: could not find libFreeImage in any of the following directories:
'/usr/local/lib/python2.6/dist-packages/mahotas-0.6.4-py2.6-linux-i686.egg/mahotas', '/lib', '/usr/lib', '/usr/local/lib', '/opt/local/lib'
I tried installing FreeImagePy and can import it with no problems but it does not help. I have tried adding to the Python path using package sys but not help either.
EDIT: I should have mentioned that all the packages except pymorph and mahotas was installed
on my linux box doing '*sudo apt-get install package_name*' while pymorph and mahotas was installed by downloading and doing 'sudo python setup.py install'.
Answer for more recent versions of mahotas
Mahotas itself does not have the functionality to read in images. imread is just a wrapper around one of 3 backends:
mahotas-imread (i.e., https://pypi.python.org/pypi/imread)
FreeImage
matplotlib (which only supports PNG & JPEG)
Thus, you need to install one of the packages above. Freeimage can be installed on Ubuntu as described below.
If you are running on Windows, you may wish to try Christoph Gohlke’s packages.
Original answer (for older versions of mahotas)
You need to install freeimage to be able to use mahotas.imread (everything else will actually work without it, it's an optional dependency). This is not a Python package per se, just a regular library.
I don't know what distribution you are in, but try looking for a freeimage package. On debian/ubuntu, you can just do:
sudo apt-get install libfreeimage3
You will have a file libfreeimage.so in /usr/lib or in a similar place and everything will work.

Categories