6

I was working with python and matplotlib but my script crashed so I had to turn off the terminal (Ubuntu 12.04, matplotib-1.1.0, python2.7). Now if I try to run any script it crashes on the line

import matplotlib.pyplot as plt

with the following error

Traceback (most recent call last):
  File "new.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-i686.egg/matplotlib/__init__.py", line 151, in <module>
    from matplotlib.rcsetup import (defaultParams,
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-i686.egg/matplotlib/rcsetup.py", line 20, in <module>
    from matplotlib.colors import is_color_like
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-i686.egg/matplotlib/colors.py", line 54, in <module>
    import matplotlib.cbook as cbook
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-i686.egg/matplotlib/cbook.py", line 32, in <module>
    import new
  File "/home/federico/Documents/doc_uni/idraulica_ambientale/relazione/scripts/variabili/new.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-i686.egg/matplotlib/pyplot.py", line 23, in <module>
    from matplotlib import _pylab_helpers, interactive
ImportError: cannot import name interactive

Notice the last line. I tried removing and reinstalling matplotlib both from source and from pip and easy_install but I can't get away with it. Same error happens if I try to import from within the python interpreter. I also installed version 1.2.0 to see if that work but it does not.

fmonegaglia
  • 2,749
  • 2
  • 24
  • 34

1 Answers1

10

If you read through files in the stack trace,

new.py-> /matplotlib/__init__.py -> matplotlib/rcsetup.py, /matplotlib/colors.py -> /matplotlib/cbook.py --> /home/federico/Documents/../new.py -> matplotlib/pyplot.py

You have named your module new which is shadowing with an import in matplolib.cbook, which is causing you to try to imort pyplot while you are importing pyplot which aparently blows up (exactly why is above my paygrade). You just need to re-name your module to something other than new.py (and remember to remove the new.pyc file that got created).

As a test run import matplotlib.pyplot as plt in an interactive shell.

FYI this is what you are shadowing.

This import will be removed in mpl 1.3

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • you are right, sorry for the mess. Anyway, when running `from matplotlib import pyplot as plt` from within the ipython shell I get the message /usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:127: RuntimeWarning: PyOS_InputHook is not available for interactive use of PyGTK set_interactive(1) – fmonegaglia Jan 03 '13 at 08:32
  • 1
    @fmonegaglia that looks like dependency issues. If you think my answer answered the question you asked, you should accept it and then open a new question with the details of the new issue. – tacaswell Jan 03 '13 at 15:04
  • also make sure the current directory in the interactive shell is not one where you have any modules defined and you have not added anything to your python path. – tacaswell Jan 03 '13 at 15:05
  • Also ensure that the new.pyc file is deleted as well – Kevin Martin Jose Apr 19 '14 at 10:51