Python、Eclipse 或 Windows 10 无法识别 matplotlib

Python、Eclipse 或 Windows 10 无法识别 matplotlib

我在 Windows 10 上使用 Eclipse。我想制作一个函数绘图仪,因此我启动了一个程序

import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import numpy as np

现在我收到一个错误:

Traceback (most recent call last):
  File "C:\Users\jaakk\eclipse-workspace\Testi\testi.py", line 3, in <module>
    from matplotlib.backends.backend_tkagg import (
ModuleNotFoundError: No module named 'matplotlib'

为什么会发生这种情况以及如何解决它,因为我已经从命令提示符运行了以下命令:

C:\Users\jaakk>pip3 install matplotlib
Requirement already satisfied: matplotlib in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (3.4.1)
Requirement already satisfied: numpy>=1.16 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (1.20.2)
Requirement already satisfied: pillow>=6.2.0 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (8.2.0)
Requirement already satisfied: pyparsing>=2.2.1 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (1.3.1)
Requirement already satisfied: cycler>=0.10 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: six in c:\users\jaakk\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from cycler>=0.10->matplotlib) (1.15.0)

C:\Users\jaakk>

答案1

为什么会发生这种情况?

您可能安装了 Python 解释器的多个副本。每个副本通常都是独立的,不共享资源(包括任何版本pip或已安装的软件包)。Eclipse 似乎指向没有的 Python 副本matplotlib

我已经pip3 install matplotlib从命令提示符运行了。

每个 Python 副本通常都会附带自己的(单独)副本,pip以管理给定安装的(单独)包。因此,可以有:

  • C:\Folder1\Python\python.exe
  • C:\Folder1\Python\Scripts\pip.exe
  • C:\Folder1\Python\Lib\site-packages\matplotlib

  • C:\Folder2\Python\python.exe
  • C:\Folder2\Python\Scripts\pip.exe
  • C:\Folder2\Python\Lib\site-packages\no matplotlib here

请注意,egpip3只能在具有一个路径的命令行中使用。

因此如果pip3引用了C:\Folder1\Python\Scripts\pip3.exe,那么matplotlib它将安装在中C:\Folder1\Python\Lib\site-packages\matplotlib。但是,如果 Eclipse 正在使用C:\Folder2\Python\python.exe来运行 Python 程序,它将查找C:\Folder2\Python\Lib\site-packages\no matplotlib here并因此无法找到matplotlib

我该如何解决这个问题?

确保 Eclipse 指向已matplotlib安装的 Python 解释器。

我建议看看(我相信)窗户优先軟件解释器找到 Eclipse 正在使用的路径并将其与来自的python.exe路径(从命令行运行)进行比较。pip3where pip3

然后你可以:

  • 将 Eclipse 中的解释器路径更改为与相同的基本文件夹匹配pip3(例如C:\Folder2\Python\python.exe可能变成C:\Folder1\Python\python.exe)。

或者

  • 编辑环境变量,使 eg与 Eclipse 使用的pip3同一个基本文件夹相关联python.exe(因此 egC:\Folder1\Python\Scripts\pip3.exe可能变为)。请注意,在这种情况下C:\Folder2\Python\Scripts\pip3.exe您需要重新运行。pip3 install matplotlib

相关内容