matplotlib - 导入错误: No module named 'matplotlib.pyplot' ; matplotlib is not a package

标签 matplotlib windows-8.1 pycharm python-3.3

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

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

对于 Matplotlib,我从这里下载了 matplotlib 和依赖项(numpy、6、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。无法导入pylab

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

最佳答案

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

关于matplotlib - 导入错误: No module named 'matplotlib.pyplot' ; matplotlib is not a package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873306/

相关文章:

python - 为什么此代码不保存带有标题的图形?

javascript - IE11 中的 localStorage 访问被拒绝,但仅在桌面模式下(不在地铁模式下)

css - woocommerce 产品白色背景仅在 Win8 上有噪音

python-2.7 - 如何在pycharm中突出显示tpl文件?

Python 计算数据透视表中的所有 bool 值

python - 创建轮廓增强漏斗图

python - 自动将maplotlib参数保存到mplstyles文件中

python - 绘制并等待 raw_input 的 Matplotlib 图形循环

mvvm - 如何在 Windows Store 8.1 MVVM 应用程序中添加命令行为

python - 为什么我的导入不再有效?