Python jupyter notebook 导入错误:没有名为“matplotlib”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43437884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:04:04  来源:igfitidea点击:

jupyter notebook import error: no module named 'matplotlib'

pythonjupyter-notebook

提问by Peter Kim

I'm an ubuntu 16.4 user and I installed anaconda3 and using both python2 and python3 kernels.

我是 ubuntu 16.4 用户,我安装了 anaconda3 并使用了 python2 和 python3 内核。

>>>jupyter kernelspec list Available kernels: python2 /home/peterkim/.local/share/jupyter/kernels/python2 python3 /home/peterkim/anaconda3/share/jupyter/kernels/python3

>>>jupyter kernelspec 列表可用内核:python2 /home/peterkim/.local/share/jupyter/kernels/python2 python3 /home/peterkim/anaconda3/share/jupyter/kernels/python3

and.. the problem was that I don't know where/how to install packages in order to for my python2 jupyter notebook not to make error 'no module named ...'. I tried pip install matplotliband conda install matplotliband I also appended '/home//anaconda2/pkgs' to the sys.path.

和.. 问题是我不知道在哪里/如何安装软件包,以便我的 python2 jupyter 笔记本不会出错“没有名为...的模块”。我试过了pip install matplotlibconda install matplotlib我还在sys.path.

(I also installed anaconda2 in search of the way of using parallel kernels. After I realised that anaconda2 was not needed. but I didn't uninstall it.)

(为了寻找并行内核的使用方式,我也安装了anaconda2。后来我意识到不需要anaconda2。但我没有卸载它。)

screenshot

截屏

thank you for helping me in advance..!

谢谢你提前帮助我..!

采纳答案by Felix

I'd recommend reading through here:

我建议阅读这里:

https://conda.io/docs/py2or3.html

https://conda.io/docs/py2or3.html

I had the same problem after installing a Python 2.7 environment inside my Anaconda3 installation. I'm not sure which command I used to create the environment, but it actually didn't install all the optional packages that are usually bundled with Anaconda (like matplotlib, numpy, ...). But that's not a big issue, you can easily install additional packages with pip as follows:

在 Anaconda3 安装中安装 Python 2.7 环境后,我遇到了同样的问题。我不确定我使用哪个命令来创建环境,但它实际上并没有安装通常与 Anaconda 捆绑在一起的所有可选包(如 matplotlib、numpy 等)。但这不是什么大问题,您可以使用 pip 轻松安装其他软件包,如下所示:

First, list your conda environments:

首先,列出您的 conda 环境:

C:\Users\Felix>conda info --envs
# conda environments:
#
ipykernel_py2            D:\Anaconda\envs\ipykernel_py2
root                  *  D:\Anaconda

Next, activate your python 2 environment using source activate <env>(Linux/OSX) or activate <env>(Windows):

接下来,使用source activate <env>(Linux/OSX) 或activate <env>(Windows)激活您的 python 2 环境:

C:\Users\Felix>activate ipykernel_py2

(ipykernel_py2) C:\Users\Felix>

Finally, use pip to install the required packages:

最后,使用 pip 安装所需的包:

(ipykernel_py2) C:\Users\Felix>pip install matplotlib
Collecting matplotlib
...
Successfully installed matplotlib-2.0.0

Hope that this helps.

希望这会有所帮助。

回答by logcat

When using python3 version of jupyter (pip3 install jupyter), matplotlib has to be installed using pip3: pip3 install matplotlib

使用python3版本的jupyter( pip3 install jupyter)时,必须使用pip3安装matplotlib:pip3 install matplotlib

回答by Backrub32

I got around it with typing the following command in the terminal:

我通过在终端中输入以下命令来解决它:

conda install matplotlib

conda install matplotlib

This will download the matplotlib package into your anaconda directory.

这会将 matplotlib 包下载到您的 anaconda 目录中。

回答by Backrub32

This worked for me on my windows 10 :

这在我的 Windows 10 上对我有用:

  1. I didn't use conda. I simply downloaded python 3.x version, then created a python 3 environment by the following command : c:\python3x\python -m venv c:\path\to\your\env.
  2. After that you can check your python version by this command python -v.
  3. Then you need to activate the python 3 environment by entering this command : env/Scripts/activate.
  4. Then install the matplotlib library by doing pip3 install matplotlib.
  1. 我没有使用康达。我只是下载了 python 3.x 版本,然后通过以下命令创建了一个 python 3 环境: c:\python3x\python -m venv c:\path\to\your\env.
  2. 之后,您可以通过此命令检查您的 python 版本python -v
  3. 然后你需要通过输入这个命令来激活 python 3 环境: env/Scripts/activate
  4. 然后通过执行安装 matplotlib 库 pip3 install matplotlib

回答by Lokesh kumar

I have checked the version of python executable and the path from where the library is getting used:

我已经检查了 python 可执行文件的版本和使用库的路径:

import sys

导入系统

sys.executable # to know the version of executable used

sys.executable # 知道所使用的可执行文件的版本

sys.path # to know from which path library is getting imported.

sys.path # 知道从哪个路径库被导入。

And then: I have installed the library in jupyter notebook cell by using pip.

然后:我已经使用 pip 在 jupyter notebook 单元中安装了库。

pip install matplotlib

pip 安装 matplotlib

After that import started working for me.

在那之后导入开始为我工作。

回答by Chatura Bashika

In my case, the matplotlib conda pkg was corrupted.

就我而言,matplotlib conda pkg 已损坏。

conda list

First, identify all the matplotlib pkgs installed in your environment. In my case, there were 2 pkgs.

首先,确定您的环境中安装的所有 matplotlib pkgs。就我而言,有 2 个包。

matplotlib

matplotlib-base

matplotlib

matplotlib 基础

Now remove those using conda.

现在删除那些使用 conda 的。

conda remove matplotlib
conda remove matplotlib-base

Now check the list again to make sure, all the pkgs removed successfully. Then reinstall them again.

现在再次检查列表以确保已成功删除所有 pkg​​。然后重新安装它们。

conda install matplotlib
conda install matplotlib-base

You may encounter an error saying

您可能会遇到错误提示

SafetyError: The package for matplotlib-base located at /home/<yourusername>/anaconda3/pkgs/matplotlib-base-3.1.3-py37hef1b27d_0 appears to be corrupted. The path 'lib/python3.7/site-packages/matplotlib-3.1.3-py3.7-nspkg.pth' has an incorrect size. reported size: 569 bytes actual size: 570 bytes

Now you need to remove this corrupted folder, in my case, "matplotlib-base-3.1.3-py37hef1b27d_0".

现在你需要删除这个损坏的文件夹,在我的例子中,“matplotlib-base-3.1.3-py37hef1b27d_0”。

Then try installing the pkgs again. It's better to run

然后再次尝试安装 pkgs。跑起来比较好

conda remove matplotlab

again before reinstalling, to make sure anything left of those pkgs completely wiped out.

在重新安装之前再次,以确保这些 pkgs 的任何剩余部分完全消失。