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

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.


Back to homepage or read more recommendations: