0

I am trying to test if Matplotlib works in PyQt on Ubuntu. I have been working with PyQt and I want to embed Matplotlib in Pyqt. I followed the code given at http://eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/ but It generates some errors while importing the matplotlib.

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_qt4agg.py", line 9, in <module>
    from matplotlib.figure import Figure
  File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 18, in <module>
    from axes import Axes, SubplotBase, subplot_class_factory
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 2, in <module>
    import math, sys, warnings, datetime, new
  File "/home/kasa/Desktop/new.py", line 25, in <module>
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
ImportError: cannot import name FigureCanvasQTAgg

I run these import commands from terminal and it works fine. Can someone figure out whats wrong with my installation.

1 Answer 1

4

Read the traceback.

You tried to import FigureCanvasQTAgg from backend_qt4agg:

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

It tried to import Figure from figure:

  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_qt4agg.py", line 9, in <module>
    from matplotlib.figure import Figure

which tried to import several things from axes:

  File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 18, in <module>
    from axes import Axes, SubplotBase, subplot_class_factory

It imports several modules as well. Notice the last one, new:

  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 2, in <module>
    import math, sys, warnings, datetime, new

and where does it look for it? Instead of the built-in module, it goes to

  File "/home/kasa/Desktop/new.py", line 25, in <module>

which, I suppose is your file and it goes back again:

    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

Python realizes that it can't import FigureCanvasQTAgg because it finds itself in a circular import hell, thus the error:

ImportError: cannot import name FigureCanvasQTAgg

Long story short

Your file masks the built-in new module. Solution is simple: Rename the file (and also remove the new.pyc from the folder).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.