ImportError: No module named matplotlib.pyplot

I am currently practicing matplotlib. This is the first example I practice.


import matplotlib.pyplot as plt

radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]

plt.plot(radius, area)

When I run this script with python ./, it shows plot correctly. However, I run it by itself, ./, it throws the followings:

Traceback (most recent call last):
File "./", line 3, in <module>
  import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

Does python look for matplotlib in different locations?

The environment is:

Mac OS X 10.8.4 64bit
built-in python 2.7

numpy, scipy, matplotlib is installed with:

sudo port install py27-numpy py27-scipy py27-matplotlib 
py27-ipython +notebook py27-pandas py27-sympy py27-nose

14 Answers

You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).


Is the standard mac python and since it doesn’t have matplotlib you should always start your script with the one installed with ports.

If python works then change the #! to:

#!/usr/bin/env python

Or put the full path to the python interpreter that has the matplotlib installed in its library.

pip will make your life easy!

Step 1: Install pip – Check if you have pip already simply by writing pip in the python console. If you don’t have pip, get a python script called , via here: or directly here: (You may have to use Save As ..)

Step 2: Take note of where the file got saved and cd the directory from command prompt. Run the script to install pip. You can write in cmd this line within quotes: “python”

Step 3: Now in cmd type: pip install matplotlib

And you should be through.

If you are using Python 2, just run

sudo apt-get install python-matplotlib

The best way to get matplotlib is :

pip install matplotlib

cause the previous way may give you a old version of matplotlib

This worked for me, inspired by Sheetal Kaul

pip uninstall matplotlib
python3 -m pip install matplotlib

I knew it installed in the wrong place when this worked:

import matplotlib

First check the version of Python

For python2:

sudo apt-get install python-matplotlib

For python3:

sudo apt-get install python3-matplotlib

If you mismatch the Matplotlib installation and the Python version you will get the no-module-error because no module for that version exits.

For python3. Just need to run pip3 install matplotlib

enter image description here

If you using Anaconda3

Just put

conda install -c conda-forge matplotlib

So I used python3 -m pip install matplotlib' thenimport matplotlib.pyplot as plt` and it worked.

I had a similar issue that I resolved and here is my issue:

I set everything up on python3 but I was using python to call my file for example: I was typing “python” …since I have everything on python3 it was thinking I was trying to use python 2.7

The correction: “python3” – the 3 made all the difference

I’m by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip is tied to python 2.7) (pip3 is tied to python 3.6)

so when installing for 2.7 do: pip install when installing for 3.6 do: pip3 install

and when running your code for 2.7 do: python when running your code for 3.6 do: python3

I hope this helps someone!

Comment in the normal feed are blocked. Let me write why this happens, just like when you executed your app.

If you ran scripts, python or ipython in another environment than the one you installed it, you will get these issues.

Don’t confuse reinstalling it. Matplotlib is normally installed in your user environment, not in sudo. You are changing the environment.

So don’t reinstall pip, just make sure you are running it as sudo if you installed it in the sudo environment.

I had a similar problem, using pip3 and all these things worked for installing matplotlib but not pyplot. This solved it for me:

import matplotlib as plt
from matplotlib import pyplot as pllt

I bashed my head on this for hours until I thought about checking my .bash_profile. I didn’t have a path listed for python3 so I added the following code:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
export PATH

And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.

The file permissions on my virtual environment directory and my project directory were not correct and, thus, would not allow me to install the proper packages. I upadated them by running:

sudo chown user:user -R [project folder]
sudo chown user:user -R [environment folder]

In the above your should use your own usernames in place of “user”. The -R recurses through all subfolders and files.

You can install the matplotlib package in python 3 by doing this

python3 -m pip install matplotlib --user

It’s working for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Privacy Policy