ImportError: No module named matplotlib.pyplot

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

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).

    /usr/bin/python

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 your_script.py 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.

From: stackoverflow.com/q/18176591

Back to homepage or read more recommendations: