ImportError: No module named matplotlib.pyplot

songsong picture songsong · Aug 11, 2013 · Viewed 614.9k times · Source

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

#!/usr/bin/python

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)
plt.show()

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

Traceback (most recent call last):
File "./plot_test.py", 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

Answer

Sheetal Kaul picture Sheetal Kaul · Mar 29, 2015

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 get-pip.py , via here: https://pip.pypa.io/en/latest/installing.html or directly here: https://bootstrap.pypa.io/get-pip.py (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 get-pip.py script to install pip. You can write in cmd this line within quotes: "python .\get-pip.py"

Step 3: Now in cmd type: pip install matplotlib

And you should be through.