Installing matplotlib on Ubuntu: ImportError


Question

My platform:

Ubuntu 13.04, Python 2.7.4.

Installing matplotlib failed, ImportError: No module named pyplot.

I have tried many ways such as

$ sudo apt-get install python-matplotlib

and easy install, install from source..., I'm folllowing http://matplotlib.org/faq/installing_faq.html

But none of them works, This ImportError always happen, Anyone can help?

EDIT The trace back:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-82be63b7783c> in <module>()
----> 1 import matplotlib

/home/wuhuijia/matplotlib.py in <module>()
      1 import numpy as np
----> 2 import matplotlib.pyplot as plt
      3 import scipy.optimize as so
      4 
      5 def find_confidence_interval(x, pdf, confidence_level):

ImportError: No module named pyplot
1
4
7/12/2013 1:05:09 AM

Accepted Answer

Your script is named matplotlib.py. Python will first look locally when importing modules, that is, on the directory itself. Thus, Python imports your script (and not the installed matplotlib) when you execute import matplotlib.pyplot, and since your script has no submodule pyplot, it fails.

Rename your script to something else (e.g., testmpl.py) and you should be fine.

8
7/12/2013 8:43:48 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon