简体   繁体   English

anaconda 无法导入 matplotlib.pyplot

[英]anaconda cannot import matplotlib.pyplot

I am getting this error when I am trying to import "matplotlib.pyplot".当我尝试导入“matplotlib.pyplot”时出现此错误。 I cant even install matplotlib.pyplot through conda install.我什至无法通过 conda install 安装 matplotlib.pyplot。

It shows this:它显示了这一点:

import matplotlib.pyplot Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'matplotlib.pyplot'导入 matplotlib.pyplot Traceback(最近一次调用最后一次):文件“”,第 1 行,在 ModuleNotFoundError:没有名为“matplotlib.pyplot”的模块

It could be that it's running your default Python installation instead of the one installed with Anaconda.可能是因为它正在运行您的默认 Python 安装,而不是与 Anaconda 一起安装的安装。 Try prepending this to the top of your script:尝试将其添加到脚本的顶部:

#!/usr/bin/env python

If that does not work, try installing matplotlib with pip , then try again:如果这不起作用,请尝试使用pip安装matplotlib ,然后重试:

pip install matplotlib

Let me know if that works for you.让我知道这是否适合您。

As reported here , when you use Anaconda, install the packet using conda.据报道在这里,当你使用Anaconda,使用畅达安装包。 In this case, the right instruction to use (on Ubuntu 18.04) is:在这种情况下,正确的使用说明(在 Ubuntu 18.04 上)是:

conda install -c conda-forge matplotlib

This will solve the problem.这将解决问题。

If you use pip (you can), you will mess up all the dependencies (and for instance, the probability that other scripts/programs do not work anymore is not null: if you use Spyder , you will have big dependencies problem to face).如果你使用pip (你可以),你会搞砸所有的依赖(例如,其他脚本/程序不再工作的可能性不是空的:如果你使用Spyder ,你将面临很大的依赖问题) .

Optional :可选

In order to always avoid problem like this, I recommend you to use Virtual Enviroment :为了始终避免这样的问题,我建议您使用Virtual Enviroment

Whats is it?这是什么?

Geeksforgeeks explains it clearly. Geeksforgeeks解释得很清楚。

How?如何?

A step-by-step guide is always useful.分步指南总是有用的。

Just open anaconda prompt and use either of the below command to install the package.只需打开 anaconda prompt 并使用以下任一命令来安装软件包。 This solved my issue.这解决了我的问题。

  1. conda install -c plotly chart-studio conda install -c plotly 图表工作室

or或者

  1. conda install -c plotly/label/test chart-studio conda install -c plotly/label/test chart-studio

I had the same issue for days, just solved it by adding "%matplotlib inline" on top of "import matplotlib.pyplot as plt"我有同样的问题好几天了,只是通过在“import matplotlib.pyplot as plt”之上添加“%matplotlib inline”来解决它

So enter this to import mathplotlib.pylot:所以输入这个来导入 mathplotlib.pylot:

%matplotlib inline
import matplotlib.pyplot as plt

Make sure Matplotlib is accessed in the Same Conda Environment as it was installed .确保在Same Conda Environment as it was installedSame Conda Environment as it was installed访问 Matplotlib。
In the case below, Matplotlib was installed in pytorch environment and not the base environment .在下面的例子中,Matplotlib 安装在pytorch environment而不是base environment
Hence when run in the pytorch environment it gets imported , but doesn't get imported in base environment .因此,当在pytorch environment运行时,它会被导入,但不会在base environment被导入

TERMINAL终端

#Installation  
(pytorch) F:\Script\Ai\Pytorch>conda install -c conda-forge matplotlib 

#Check installation  in pytorch Environment  
(pytorch) F:\Script\Ai\Pytorch>python  
>>> import matplotlib
>>> print('matplotlib: {}'.format(matplotlib.__version__))
matplotlib: 3.3.4  


#Import Error in base Environment
(base) F:\Script\Ai\Pytorch>python
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'

Switch to the correct environment before installing.安装前切换到正确的环境。

conda activate environment_name_here
conda install matplotlib

In my case, I had an environment for a jupyter notebook, but installed in initially in a new terminal window which defaulted to the base environment.就我而言,我有一个用于 jupyter 笔记本的环境,但最初安装在默认为base环境的新终端窗口中。

Check if the ...../python3.x/site-packages is listed within sys.path .检查...../python3.x/site-packages是否在sys.path中列出。 If not append it with sys.path.append('.....python3.8/site-packages')如果不附加sys.path.append('.....python3.8/site-packages')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM