anaconda cannot import matplotlib.pyplot

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'


Solution 1:

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.

Solution 2:

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:

Whats is it?

Geeksforgeeks explains it clearly.

How?

A step-by-step guide is always useful.