无法在 Jupyter Notebook 中导入 matplotlib,而它看起来实际上已正确安装在正确的 Python 版本中

Not able to import matplotlib in Jupyter Notebook while it looks actually installed correctly in the correct Python version

在查看了 Whosebug 中同一问题的许多答案后,我必须认识到我面临着一种奇怪的情况。 我在 Linux Ubuntu 18.04.6 LTS。 在我的 jupyter notebook 中,我得到以下配置:

import sys
print(sys.version)
3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 23:01:00) 
[GCC 7.3.0]

sys.path
['/home/hector/_NOTEBOOKS',
 '',
 '/home/hector/.local/lib/python3.7/site-packages',
 '/snap/jupyter/6/lib/python37.zip',
 '/snap/jupyter/6/lib/python3.7',
 '/snap/jupyter/6/lib/python3.7/lib-dynload',
 '/home/hector/snap/jupyter/common/lib/python3.7/site-packages',
 '/snap/jupyter/6/lib/python3.7/site-packages',
 '/snap/jupyter/6/lib/python3.7/site-packages/IPython/extensions',
 '/home/hector/snap/jupyter/6/.ipython'] 

但是当我尝试导入 matplotlib 时:

import matplotlib
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-6-0484cd13f94d> in <module>
----> 1 import matplotlib

ModuleNotFoundError: No module named 'matplotlib'

现在,当我打开 终端控制台并实际启动 python3.7

hector@lenovo2:/snap/jupyter/6/bin$ ./python3.7
>>> import matplotlib
>>> import sys
>>> sys.path
['', '/snap/jupyter/6/lib/python37.zip', '/snap/jupyter/6/lib/python3.7', '/snap/jupyter/6/lib/python3.7/lib-dynload', '/home/hector/.local/lib/python3.7/site-packages', '/snap/jupyter/6/lib/python3.7/site-packages']

>>> matplotlib.__path__
['/home/hector/.local/lib/python3.7/site-packages/matplotlib']

/home/hector/.local/lib/python3.7/site-packages/ 目录中 python3.7 的 matplotlib 似乎已正确安装。 在 jupyter notebook 中,这被列为 jupyter python 版本(3.7)的 sys.path 中的第一个条目,尽管有这种配置,但令我最绝望的是,jupyter notebook 找不到 matplotlib ...

我感谢所有解决这个难题的帮助和想法:-)

谢谢

我尝试按照对我的问题的评论继续进行,并建议使用 pip3 安装 jupyter 和所有其他软件。

我对我的整体安装有疑问,因为我的系统上没有 'pip3' 可用的东西。

事实上,这个问题与我最初安装 jupyter notebook 和 ubunutu 提供的 apt 包有关,我实际上不应该

这个 Ubuntu apt 软件包提供了一个 'snap' 安装,它非常有界并且与计算机上的其他 python 实例分开。它 'messes up' python, 确实 遇到了我问题中描述的情况。

我的第一步是删除 jupyter 的 apt 安装:

> sudo apt remove jupyter-notebook
> sudo apt remove jupyter
> sudo apt autoremove

我的第二步是 为我当前的 python3 安装安装 pip3:

> sudo apt install python3-pip

我的第三步是使用 pip3 安装笔记本(而不是使用 Ubuntu 的 apt 包):

> pip3 install jupyter

最后,我可以安装一个可以从 jupyter 笔记本中导入的 matplotlib,使用 pip3(注意,作为先决条件,必须安装一些依赖项):

#Prerequisite dependencies
> sudo apt install zlib1g-dev libncurses5-dev

# THE HAPPY END FINALLY COMES WITH:

> pip3 install matplotlib

#(claps and happiness here)

干杯