2

Please forgive my simple question. I have just started to use Matplotlib and I am having some difficulty.

I can run the following with the interpretor without problems:

>>> from pylab import *  
>>> plot([1,2,3])  
>>> show()  

The above code generates a beautiful graph.

However, if I place the following code inside a file and run it, I get an error:

#!/usr/bin/env python
# encoding: utf-8

import sys
import os
from pylab import *

plot([1,2,3])
show()

Here is the error message:

Traceback (most recent call last):
  File "/Users/sbrown/Desktop/new1.py", line 12, in <module>
    from pylab import *
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 133, in <module>
    from matplotlib.rcsetup import (defaultParams,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/rcsetup.py", line 19, in <module>
    from matplotlib.colors import is_color_like
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/colors.py", line 54, in <module>
    import matplotlib.cbook as cbook
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook.py",     line 15, in <module>
    import new
  File "/Users/sbrown/Desktop/new.py", line 2, in <module>
    plot([1,2,3])
NameError: name 'plot' is not defined
>>> 

Any idea what the problem could be? Thanks in advance for any help you can provide!

4
  • It might be a namespace issue. What happens if you get rid of the from pylab import * and use import pylab pylab.plot([1,2,3])? Commented May 12, 2011 at 18:48
  • It looks like the error message is not related to the code snippet you've posted: Where does "import new" comes from? Do you have a new.py file on your desktop? Commented May 12, 2011 at 18:48
  • @ Stephen Terry I tried your suggestion. No luck, it throws the same error. I appreciate the help though! Commented May 12, 2011 at 18:59
  • @ Zaur Nasibov Correct, the file name is new.py and it is on my Desktop. Commented May 12, 2011 at 19:00

1 Answer 1

9

Looks like you have a file on your Desktop that is shadowing the standard Python new module:

>>> import new
>>> new
<module 'new' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/new.pyc'>

Rename or remove $HOME/Desktop/new.py and try again.

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

2 Comments

this worked! thanks a million samplebias! lesson learned, do not tangle with reserved words!!!!
No sweat, I think all Python-ers run into this sooner or later, e.g. you start picking generic names for modules, like string or xml and run into trouble when they conflict.

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.