ImportError:没有名为'matplotlib.pyplot'的模块; matplotlib不是一个包

时间:2014-05-26 15:22:48

标签: matplotlib windows-8.1 pycharm python-3.3

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

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

对于Matplotlib,我从这里下载了matplotlib和依赖项(numpy,six,dateutil,pyparsing,pytz)(Python 3.3的版本):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变量,但这导致了另一个错误。

1 个答案:

答案 0 :(得分:0)

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