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
Solution 1:
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.
Solution 2:
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.
Solution 3:
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