jupyter notebook import error: no module named 'matplotlib'

For those still looking for a solution, especially using virtualenv, this worked for me:

1 - Inside your project directory, create a virtual environment. You may have to install virtualenv in case you don't have it

virtualenv myenv --python=python3.7

2 - Install matplotlib inside of your virtual env:

pip3 install matplotlib

3 - Install ipykernel inside your virtual env

pip3 install ipykernel

4 - Connect your jupyter kernel to your new environment. You may have to use sudo here

python3 -m ipykernel install --name=myenv

5 - When you start your jupyter lab, you will have the option to select your env, which has matplotlib installed

enter image description here


I'd recommend reading through here:

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:

First, list your conda environments:

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):

C:\Users\Felix>activate ipykernel_py2

(ipykernel_py2) C:\Users\Felix>

Finally, use pip to install the required packages:

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

Hope that this helps.


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