Skip to main content
 首页 » 编程设计

python之导入错误 : No module named pyplot (even after installing matplotlib)

2025年05月04日22mfryf

我正在尝试像这样导入 matplotlib:

import matplotlib.pyplot as plt 

为此我安装了 matplotlib 使用。

python -m pip install -U pip setuptools 
python -m pip install matplotlib 

尽管 basic_example.py 来自 the examples工作,它突然停止与 matplotlib 的所有其他文件一起运行,给我以下错误:

File "matplotlib.py", line 2, in <module> 
  import matplotlib.pyplot as plt # pyplot module. 
ImportError: No module named pyplot` 

我试着检查模块是否还在,它似乎还在。可能有什么问题?

Requirement already satisfied: matplotlib in c:\python27\lib\site-packages 
Requirement already satisfied: numpy>=1.6 in c:\python27\lib\site-packages (from matplotlib) 
Requirement already satisfied: python-dateutil in c:\python27\lib\site-packages (from matplotlib) 
Requirement already satisfied: cycler in c:\python27\lib\site-packages (from matplotlib) 
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,>=1.5.6 in c:\python27\lib\site-packages (from matplotlib) 
Requirement already satisfied: pytz in c:\python27\lib\site-packages (from matplotlib) 
Requirement already satisfied: six>=1.5 in c:\python27\lib\site-packages (from python-dateutil->matplotlib)` 

我正在使用 Windows 和 Python 2.7。

请您参考如下方法:

Python 2.7 会先尝试在当前工作目录中查找模块,然后再进入已安装的包,因此因为您将测试文件命名为“matplotlib.py”,它会将其解释为 matplotlib 包。将您的测试文件重命名为其他名称,应该没问题