ImportError: no module named matplotlib.pyplot

I am currently practicing matplotlib. This is the first example that 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 the graph correctly. However, I run it on my own, ./plot_test.py , it produces the following:

 Traceback (most recent call last): File "./plot_test.py", line 3, in <module> import matplotlib.pyplot as plt ImportError: No module named matplotlib.pyplot 

Do python look at matplotlib in different places?

Wednesday:

 Mac OS X 10.8.4 64bit built-in python 2.7 

numpy, scipy, matplotlib is installed using:

 sudo port install py27-numpy py27-scipy py27-matplotlib \ py27-ipython +notebook py27-pandas py27-sympy py27-nose 
+101
python matplotlib
Aug 11 '13 at 20:54 on
source share
7 answers

Two pythons are installed on your computer: one is the standard python that comes with Mac OSX, and the second is the one you installed with the ports (this is the one that has matplotlib installed in its library, the one that comes with macosx is not).

 /usr/bin/python 

This is the standard mac python and since it does not have matplotlib , you should always run your script with the one installed with the ports.

If python your_script.py working, change the value to #! on:

 #!/usr/bin/env python 

Or put the full path to the python interpreter that has matplotlib installed in its library.

+32
Aug 11 '13 at 21:13
source share

pip will make your life easy!

Step 1: Install pip. Check if you have pip just by writing pip in the python console. If you don't have a pip, get a python script named get-pip.py, here: https://pip.pypa.io/en/latest/installing.html or right here: https://bootstrap.pypa.io/ get-pip.py (You may need to use Save As ..)

Step 2: pay attention to where the file was saved, and cd from the command line. Run get-pip.py script to install pip. You can write this line in quotes in cmd: "python. \ Get-pip.py"

Step 3: Now in cmd format: pip install matplotlib

And you have to get through.

+117
Mar 29 '15 at 21:40
source share

If you are using Python 2, just run

 sudo apt-get install python-matplotlib 

Best way to get matplotlib :

 pip install matplotlib 

the reason the previous method might give you the old version of matplotlib

+25
Aug 11 '17 at 7:19 on
source share

It worked for me, inspired by Sheetal Kaul

 pip uninstall matplotlib python3 -m pip install matplotlib 

I knew that it was not installed in the place where it worked:

 python2.7 import matplotlib 
+8
Jun 17 '18 at 19:12
source share

I had a similar problem that I solved, and here is my problem:

I installed everything on python3, but I used python to call my file, for example: I typed "python mnist.py" ... since I have everything on python3, I thought I was trying to use python 2.7

Bugfix: "python3 mnist.py" - 3 made all the differences

I am by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip linked to python 2.7) (pip3 linked to python 3.6)

so when installing for 2.7 do: pip install when installing for 3.6 do: pip3 install

and when running code for 2.7 do: python when running code for 3.6 do: python3

I hope this helps someone!

+1
Aug 30 '18 at 16:35
source share

I rolled my head for hours until I thought about checking my .bash_profile. I did not have a path 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 reinstall matplotlib with sudo pip3 install matplotlib . Everything is working fine now.

0
Aug 16 '17 at 11:49 on
source share

Comments in the normal feed are blocked. Let me write why this happens, just like when you started your application.

If you ran the scripts, python or ipython in a different environment than the one in which you installed them, you will get these problems.

Do not confuse reinstallation. Matplotlib is usually installed in your user environment, and not in sudo. You are changing the environment.

So don't reinstall pip, just make sure you use it as sudo if you installed it in a sudo environment.

0
Apr 25 '19 at 16:51
source share



All Articles