在研究了堆叠溢出中相同问题的许多答案之后,我必须认识到我正面临着一个奇怪的情况。我在LinuxUbuntu18.04.6LTS上。在我的jupyter笔记本中,我得到了以下配置:
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']看起来,matplotlib都在/home/hector/.local/lib/python3.7/site-packages/目录中为python3.7正确安装了。在jupyter笔记本中,这是jupyter的python版本(3.7)在sys.path中的第一个条目,尽管有这种配置,而且让我失望的是,jupyter笔记本找不到matplotlib.
我感谢为解决这一困境所提供的帮助和想法:)
谢谢
发布于 2022-03-23 07:07:01
我试着按照对我的问题的评论继续,并建议安装jupyter和所有其他的使用pip3。
我对我的整体安装有一个疑问,因为我的系统上没有任何可用的“pip3”。
事实上,这个问题与我最初安装的jupyter笔记本和Ubunutu提供的apt包有关,而我的实际上不应该有。
这个Ubuntu包提供了一个“snap”安装,它与计算机上的其他python实例有很大的距离。它“搞砸了”python,就像我的问题中描述的那样,一个得到了这样的情况。
我的第一步是删除jupyter的apt安装。
> sudo apt remove jupyter-notebook
> sudo apt remove jupyter
> sudo apt autoremove我的第二步是为当前的安装安装pip3
> sudo apt install python3-pip我的第三步是使用安装pip3笔记本(而不是使用Ubuntu的apt包):
> pip3 install jupyter最后,我可以使用安装一个可以从jupyter笔记本中导入的matplotlib (注意,必须安装一些依赖项作为先决条件):
#Prerequisite dependencies
> sudo apt install zlib1g-dev libncurses5-dev
# THE HAPPY END FINALLY COMES WITH:
> pip3 install matplotlib
#(claps and happiness here)干杯
https://stackoverflow.com/questions/71549168
复制相似问题