'jupyter notebook import error: no module named 'matplotlib'
I'm an ubuntu 16.4 user and I installed anaconda3 and using both python2 and python3 kernels.
>>>jupyter kernelspec list Available kernels: 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 matplotlib and conda install matplotlib and I also appended '/home//anaconda2/pkgs' to the 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.)
Solution 1:[1]
When using python3 version of jupyter (pip3 install jupyter), matplotlib has to be installed using pip3: pip3 install matplotlib
Solution 2:[2]
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
Solution 3:[3]
I got around it with typing the following command in the terminal:
conda install matplotlib
This will download the matplotlib package into your anaconda directory.
Solution 4:[4]
This worked for me on my windows 10 :
- 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. - After that you can
check your python version by this command
python -v. - Then you need to activate the python 3 environment by entering this command :
env/Scripts/activate. - Then install the matplotlib library by doing
pip3 install matplotlib.
Solution 5:[5]
I have checked the version of python executable and the path from where the library is getting used:
import sys
sys.executable # to know the version of executable used
sys.path # to know from which path library is getting imported.
And then: I have installed the library in jupyter notebook cell by using pip.
pip install matplotlib
After that import started working for me.
Solution 6:[6]
In my case, the matplotlib conda pkg was corrupted.
conda list
First, identify all the matplotlib pkgs installed in your environment. In my case, there were 2 pkgs.
matplotlib
matplotlib-base
Now remove those using 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.
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".
Then try installing the pkgs again. It's better to run
conda remove matplotlab
again before reinstalling, to make sure anything left of those pkgs completely wiped out.
Solution 7:[7]
In windows OS, I found @melkorCba suggestion helpful. But, I have some edits:
- After you type
conda listin conda console, you can see matplotlib listed. Do not uninstall the module yet. - Go to conda GUI, launch Jupyter, clear Kernel and run the notebook again. To clear Kernel, in the Jupyter notebook, go to kernel tab > Restart and Clear Output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | lucianokrebs |
| Solution 3 | Backrub32 |
| Solution 4 | barbsan |
| Solution 5 | Lokesh kumar |
| Solution 6 | Community |
| Solution 7 | annu thapa |


