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?
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.
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”
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 mnist.py” …since I have everything on python3 it was thinking I was trying to use python 2.7
The correction:
“python3 mnist.py” – 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
# Setting PATH for Python 3.6# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
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
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.