ImportError:没有名为"matplotlib.pyplot"的模块;matplotlib不是软件包

问题描述:

我正在尝试使用matplotlib从ECG信号进行实时分析,但是问题甚至在更早之前就开始出现.

I am trying to use matplotlib for real-time analysis from ECG-signals, but the problem starts even before.

我使用的是PyCharm IDE,目前可用于Python 3.3,而我的操作系统是Windows 8.1.

I use the PyCharm IDE, currently working with Python 3.3 and my os is Windows 8.1.

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

For Matplotlib I downloaded matplotlib and the dependencies (numpy, six, dateutil, pyparsing, pytz) from here (the versions for Python 3.3): http://www.lfd.uci.edu/~gohlke/pythonlibs/ and installed it in the Python33 folder.

现在,如果我尝试:

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()

对于一个小测试,我得到两个不同的错误.对于第一个,如下所示:

For a smal test, I get two different Error's. For the first one it is the following:

 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  

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

And for the second bigger example it is this:

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.无法导入pylab

Afterwards I changed the imports to the ones Pycharm wanted me to use. from matplotlib import pylab but this only resulted in an ImportError. cannot import pylab

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

The funny thing is, if I run these small tests in the Python Console it works just fine, so my guess is that it has something to do with PyCharm... I also tried to add the exact path from the matplotlib to the Path variable but that resulted in another Error.

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

Your current project folder C:/Users/Timo/PycharmProjects/GUI_test/matplotlib.py contains matplotlib.py which causes this issue. Change the filename to anything else, which is not a name of a python package.