【问题标题】:ImportError: No module named 'matplotlib.pyplot'; matplotlib is not a packageImportError:没有名为“matplotlib.pyplot”的模块; matplotlib 不是一个包
【发布时间】:2014-07-15 09:57:48
【问题描述】:

我正在尝试使用 matplotlib 对 ECG 信号进行实时分析,但问题甚至在此之前就开始了。

我使用 PyCharm IDE,目前使用 Python 3.3,我的操作系统是 Windows 8.1。

对于 Matplotlib,我从这里(Python 3.3 的版本)下载了 matplotlib 和依赖项(numpy、6、dateutil、pyparsing、pytz):http://www.lfd.uci.edu/~gohlke/pythonlibs/ 并将其安装在 Python33 文件夹中。

现在如果我尝试:

from matplotlib.pyplot import plot, show  

    plot(range(10))  
    show()

或:

import pylab
from pylab import *

xAchse=pylab.arange(0,100,1)
yAchse=pylab.array([0]*100)

fig = pylab.figure(1)
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_title("Realtime Waveform Plot")
ax.set_xlabel("Time")
ax.set_ylabel("Amplitude")
ax.axis([0,100,-1.5,1.5])
line1=ax.plot(xAchse,yAchse,'-')

manager = pylab.get_current_fig_manager()

values=[]
values = [0 for x in range(100)]

Ta=0.01
fa=1.0/Ta
fcos=3.5

Konstant=cos(2*pi*fcos*Ta)
T0=1.0
T1=Konstant

def SinwaveformGenerator(arg):
  global values,T1,Konstant,T0
  #ohmegaCos=arccos(T1)/Ta
  #print "fcos=", ohmegaCos/(2*pi), "Hz"

  Tnext=((Konstant*T1)*2)-T0
  if len(values)%100>70:
    values.append(random()*2-1)
  else:
    values.append(Tnext)
  T0=T1
  T1=Tnext

def RealtimePloter(arg):
  global values
  CurrentXAxis=pylab.arange(len(values)-100,len(values),1)
  line1[0].set_data(CurrentXAxis,pylab.array(values[-100:]))
  ax.axis([CurrentXAxis.min(),CurrentXAxis.max(),-1.5,1.5])
  manager.canvas.draw()
  #manager.show()

timer = fig.canvas.new_timer(interval=20)
timer.add_callback(RealtimePloter, ())
timer2 = fig.canvas.new_timer(interval=20)
timer2.add_callback(SinwaveformGenerator, ())
timer.start()
timer2.start()

pylab.show()

对于一个小测试,我得到两个不同的错误。第一个是这样的:

 Traceback (most recent call last):  
  File "<frozen importlib._bootstrap>", line 1519, in _find_and_load_unlocked  
AttributeError: 'module' object has no attribute __path__      
During handling of the above exception, another exception occurred:  
Traceback (most recent call last):  
  File "C:/Users/Timo/PycharmProjects/GUI_test/matplotlib.py", line 1, in <module>
    from matplotlib.pyplot import plot, show  
  File "C:\Users\Timo\PycharmProjects\GUI_test\matplotlib.py", line 1, in <module>
    from matplotlib.pyplot import plot, show  
ImportError: No module named 'matplotlib.pyplot'; matplotlib is not a package  

第二个更大的例子是这样的:

Traceback (most recent call last):
  File "C:/Users/Timo/PycharmProjects/GUI_test/matplotlib.py", line 1, in <module>
    import pylab
  File "C:\Python33\lib\site-packages\pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "C:\Users\Timo\PycharmProjects\GUI_test\matplotlib.py", line 4, in <module>
    xAchse=pylab.arange(0,100,1)
AttributeError: 'module' object has no attribute 'arange'

之后,我将导入更改为 Pycharm 希望我使用的导入。 from matplotlib import pylab 但这只会导致ImportError. cannot import pylab

有趣的是,如果我在 Python 控制台中运行这些小测试,它工作得很好,所以我的猜测是它与 PyCharm 有关...... 我还尝试将 matplotlib 中的确切路径添加到 Path 变量中,但这导致了另一个错误。

【问题讨论】:

    标签: matplotlib windows-8.1 pycharm python-3.3


    【解决方案1】:

    您当前的项目文件夹C:/Users/Timo/PycharmProjects/GUI_test/matplotlib.py 包含导致此问题的matplotlib.py。将文件名更改为其他任何名称,而不是 python 包的名称。

    【讨论】:

    • 我不知道这是个问题。我搜索了一个小时或更长时间,这是第一个提到这个潜在解决方案的答案。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2019-07-07
    • 2018-10-16
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2016-02-26
    相关资源
    最近更新 更多