首页 > 解决方案 > 即使已安装,导入 matplotlib 也会出错

问题描述

我希望有人可以帮助我。

系统是 MacOS Catalina。Python 3.8 集成开发环境:VS

我使用 pip install matplotlib 安装了 matplotlib,我看到它卡在 python2.7 下。我卸载了 matplotlib 并使用 pip3 install matplotlib 重新安装它并收到以下消息:

Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.7/site-packages (from matplotlib) (1.18.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/site-packages (from matplotlib) (2.4.6)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/site-packages (from matplotlib) (1.1.0)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: six in /Users/apple/Library/Python/3.7/lib/python/site-packages (from cycler>=0.10->matplotlib) (1.13.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib) (46.0.0)

但是,当我尝试使用“import matplotlib”运行我的 .py 文件时,会出现以下错误:

    import matplotlib as mpl 
ModuleNotFoundError: No module named 'matplotlib'

我已经尝试为解决方案搜索不同的来源,但我现在被卡住了。

标签: pythonpython-3.xmatplotlib

解决方案


您主要使用命令运行 python 脚本python your_file.py,尝试python3 your_file.py在终端中运行。pip3 在 python3 下安装包,而不是默认的 python2。

这是因为我相信您的 mac 终端默认使用 python 2.x,因此该命令python使用 python 2.x 版本并python3使用可用于执行文件的 3.x 版本。因此,如果您想在 python 2.x 下安装任何包,请使用pip命令并在 python 3.x 下安装包,请使用pip3命令


推荐阅读