Reputation: 121
I am getting this error when I am trying to import "matplotlib.pyplot". I cant even install matplotlib.pyplot through conda install.
It shows this:
import matplotlib.pyplot Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'matplotlib.pyplot'
Upvotes: 11
Views: 39180
Reputation: 3715
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 it initially in a new terminal window which defaulted to the base
environment.
Upvotes: 1
Reputation: 462
Check if the ...../python3.x/site-packages
is listed within sys.path
. If not append it with sys.path.append('.....python3.8/site-packages')
Upvotes: 1
Reputation: 1551
Make sure Matplotlib is accessed in the Same Conda Environment as it was installed
.
In the case below, Matplotlib was installed in pytorch environment
and not the base environment
.
Hence when run in the pytorch environment
it gets imported, but doesn't get imported in 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'
Upvotes: 1
Reputation: 51
I had the same issue for days, just solved it by adding "%matplotlib inline" on top of "import matplotlib.pyplot as plt"
So enter this to import mathplotlib.pylot:
%matplotlib inline
import matplotlib.pyplot as plt
Upvotes: 5
Reputation: 5647
As reported here, when you use Anaconda, install the packet using conda. In this case, the right instruction to use (on Ubuntu 18.04) is:
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).
Optional:
In order to always avoid problem like this, I recommend you to use Virtual Enviroment:
Geeksforgeeks explains it clearly.
A step-by-step guide is always useful.
Upvotes: 6
Reputation: 59
Just open anaconda prompt and use either of the below command to install the package. This solved my issue.
or
Upvotes: 2
Reputation: 1752
It could be that it's running your default Python installation instead of the one installed with 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 install matplotlib
Let me know if that works for you.
Upvotes: 4