0

I am fairly new to python as well as matplotlib and I can't get it to work. From the code :

import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node("spam")
G.add_edge(1,2)
plt.show()

I get the error:

Traceback (most recent call last): 
    File "test.py2", line 2, in <module>
        import matplotlib.pyplot as plt 
ImportError: No module named matplotlib.pyplot

This occurs even though it seems to be installed in:

usr/lib/python2.7/dist-packages/matplotlib/

Do you guys have any ideas? Thanks in advance

4
  • I am running Ubuntu 16.04.1 LTS Commented Aug 23, 2016 at 14:25
  • Check if your file is hitting the correct python directory. Commented Aug 23, 2016 at 14:26
  • 1
    type which python to check your python path Commented Aug 23, 2016 at 14:36
  • from which python I get /usr/local/bin/python Commented Aug 23, 2016 at 14:38

3 Answers 3

1

You have 2 pythons installed on your machine, one is the standard python that comes with MacOSX 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 shebang (#!) to:

\#!/usr/bin/env python

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

Sign up to request clarification or add additional context in comments.

3 Comments

I am running Ubuntu 16.04.1 LTS not MacOS, do I still have 2 python directories?
Yes MacOS and Linux OS have the same architecture.
Even when I go to the direct directory, containing python2.7 I still get the same error...
1

thanks for your help. It appeared the wrong Python Version was used. By using

alias python=/usr/lib/python

it was fixed, but only temporarly.

To permanently set the alias correctly, I had to edit the ~/.bash_aliases and insert:

alias python=/usr/bin/python2.7

The other python version installed was 3.0 which was set as the defualt one, but without the matplotlib library.

1 Comment

Hi @JKP, you can accept your own answer so others know you have solved the problem - click on the check mark to the left of the post! It would be useful to future readers if you could describe more about the solution, too (like which other versions you had, and where it was originally pointing to).
0

You can check whether usr/lib/python2.7/dist-packages (if you are pretty sure matplotlib is installed here) is in your sys.path.

>>> import sys
>>> sys.path

If you don't find the path in the list, you can add lines below before importing matplotlib.

import sys
sys.path.insert(0, '/path/to/matplotlib')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.