0

我正在尝试测试 Matplotlib 是否在 Ubuntu 上的 PyQt 中工作。我一直在使用 PyQt,我想在 Pyqt 中嵌入 Matplotlib。我遵循了http://eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/给出的代码,但是在导入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

我从终端运行这些导入命令,它工作正常。有人可以弄清楚我的安装有什么问题。

4

1 回答 1

4

阅读回溯。

您尝试FigureCanvasQTAgg从以下位置导入backend_qt4agg

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

它试图Figure从以下位置导入figure

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

它试图从以下位置导入一些东西axes

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

它还导入了几个模块。注意最后一个,new

  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

Python 意识到它无法导入FigureCanvasQTAgg,因为它发现自己处于循环导入地狱中,因此出现错误:

ImportError: cannot import name FigureCanvasQTAgg

长话短说

您的文件掩盖了内置new模块。解决方案很简单:重命名文件(并new.pyc从文件夹中删除)。

于 2012-06-30T06:40:41.300 回答