24

I am currently trying to work basic python - jupyter projects.

I am stuck on following error during matplotlib:

screenshot on jupyter-error enter image description here

ModuleNotFoundError: No module named 'matplotlib'

I tried to update, reinstall matplotlib aswell in conda and in pip but it still not working.

happy over every constructive feedback

Renats Stozkovs
  • 2,479
  • 10
  • 21
  • 25
Bullfraud
  • 261
  • 1
  • 2
  • 6
  • 5
    I bet your `ipython kernel` is pointing to a python version without a `matplotlib` installation. – Abdou Feb 19 '17 at 00:03
  • You need to install modules in the environment that pertains to the select kernel for your notebook. At the top right, it should indicate which kernel you are using. Go to "Kernel" --> "Change Kernels" and try selecting a different one, e.g. "Root". – pylang Feb 19 '17 at 04:10
  • 1
    In some situations, even with the correct kernel activated (where the kernel has matplotlib installed), it can still fail to locate the package. If you've tried all the other methods mentioned in this thread and still cannot get it to work, consider installing it directly within the jupyter notebook cell with ```!pip install matplotlib``` – khuang834 Jun 28 '20 at 16:46

8 Answers8

28

In a Notebook's cell type and execute the code:

import sys  
!{sys.executable} -m pip install --user matplotlib

and reload the kernel

(src: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/ )

Frédéric
  • 281
  • 3
  • 3
5

open terminal and change the directory to Scripts folder where python installed. Then type the following command and hit enter

pip install matplotlib

Hope this will solve the issue.

Shahriar Miraj
  • 178
  • 1
  • 14
  • 3
    If I do this, I get `Requirement already satisfied`. But in python I still get `module not found`... – Kokodoko Jul 08 '21 at 15:46
3

I was facing the exact issue. It turns out that it was using the system Python version despite me having activated my virtual environment.

This is what eventually worked.

If you are using a virtual environment which has a name say myvenv, first activate it using command:

source activate myvenv

Then install module ipykernel using the command:

pip install ipykernel

Finally run (change myvenv in code below to the name of your environment):

ipykernel install --user --name myvenv --display-name "Python (myvenv)" 

Now restart the notebook and it should pick up the Python version on your virtual environment.

hahnec
  • 412
  • 5
  • 11
bhaskarc
  • 8,714
  • 10
  • 61
  • 83
2

The issue with me was that jupyter was taking python3 for me, you can always check the version of python jupyter is running on by looking on the top right corner (attached screenshot).

enter image description here

When I was doing pip install it was installing the dependencies for python 2.7 which is installed on mac by default. It got solved by doing:

> pip3 install matplotlib
Siddharth Sharma
  • 1,575
  • 1
  • 16
  • 32
1

Having the same issue, installing matplotlib before to create the virtualenv solved it for me. Then I created the virtual environment and installed matplotlib on it before to start jupyter notebook.

0
  1. in jupter notebook type

print(sys.executable)

this gave me the following /Users/myusername/opt/anaconda3/bin/python

  1. open terminal, go into the folder /Users/myusername/opt/anaconda3/bin/

  2. type the following: python3 -m pip install matplotlib

  3. restart jupyter notebook (mine is vs code mac ox)

Fen
  • 51
  • 1
  • 1
0

generally speaking you should try to work within python virtual environments. and once you do that, you then need to tell JupyterLab about it. for example:

# create a virtual environment
# use the exact python you want to work with in this step
python3.9 -m venv myvenv
# 'activate' (or 'enter') it
source myvenv/bin/activate
# install the exact stuff you want to use in that environment
pip install matplotlib
# now tell JupyterLabs about the environment
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
# start it up 
jupyter notebook mynotebook
# if you now look under 'Kernel->Change kernel', your 'myenv' should be there
# select it (restart kernel etc if needed) and you should be good
-1

Check the python version:

$python --version

or

$python3 --version

Try installing "matplotlib" using sudo:

For python version 2.7

$sudo pip install matplotlib

or

For python version 3.x

$sudo pip3 install matplotlib
Biranchi
  • 15,670
  • 23
  • 121
  • 160