Can't import matplotlib.pyplot in Sage 6.2

477 views
Skip to first unread message

Jim Clark

unread,
May 27, 2014, 7:04:08 AM5/27/14
to sage-s...@googlegroups.com
Hello,

A script that I wrote in April under Sage 6.1.1 stopped working; attempting to import matplotlib.pyplot produces an error:

Jims-computer:HW2 jim$ sage
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.2, Release Date: 2014-05-06                         │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: import matplotlib.pyplot as mpl
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-0309ea03fddd> in <module>()
----> 1 import matplotlib.pyplot as mpl

/Applications/sage/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/pyplot.py in <module>()
     96 
     97 from matplotlib.backends import pylab_setup
---> 98 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
     99 
    100 

/Applications/sage/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/__init__.pyc in pylab_setup()
     26     # imports. 0 means only perform absolute imports.
     27     backend_mod = __import__(backend_name,
---> 28                              globals(),locals(),[backend_name],0)
     29 
     30     # Things we pull in from all backends

/Applications/sage/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/backend_tkagg.py in <module>()
      9 
     10 # Paint image to Tk photo blitter extension
---> 11 import matplotlib.backends.tkagg as tkagg
     12 
     13 from matplotlib.backends.backend_agg import FigureCanvasAgg

/Applications/sage/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/tkagg.py in <module>()
      1 from __future__ import print_function
----> 2 from matplotlib.backends import _tkagg
      3 import Tkinter as Tk
      4 
      5 def blit(photoimage, aggimage, bbox=None, colormode=1):

ImportError: cannot import name _tkagg
sage: 

Downloaded and installed the Sage 6.2 binary on OSX 10.9.3.

Seems like a regression error, but I don’t know how to fix it…

Please help.
Thank you,
Jim Clark

kcrisman

unread,
May 27, 2014, 12:33:15 PM5/27/14
to sage-s...@googlegroups.com
Hi!  Is it possible that some locations changed in your installation?  Here is an old post that has a very similar error:

Otherwise, perhaps the problem may be the one described here:
in which case the binary was probably compiled without tkagg support, which is normal.  You could Compile Sage from scratch, perhaps could redo matplotlib.  See also http://ask.sagemath.org/question/3962/gui-backend-matplotlib-in-sage

Now, we did upgrade matplotlib in http://trac.sagemath.org/ticket/14993 so I wouldn't be surprised if that did cause some problems (there were several tickets fixed) but it is a little surprising that this is the one, since we have this same line in the documentation at 

I hope that ONE of these things helps, but I'm sorry if not - in that event, any other platform details would be very helpful for us.

Jim Clark

unread,
May 27, 2014, 6:09:58 PM5/27/14
to sage-s...@googlegroups.com
Thank you for the response, kcrisman (sorry I don’t know your first name).

Configuration: Apple MacBook pro running OSX 10.9.3
Downloaded and installed vanilla sage-6.2-x86_64-Darwin-OSX_10.9_x86_64.dmg
I have set export SAGE_MATPLOTLIB_GUI=no in my .bash_profile (due to a similar situation in the past. I only want to use matplotlib to save to .png files, not to view interactively.) I am happy with Sage being built without tkagg support.
(I also have python, scipy, matplotlib, etc. installed via macports, but Sage has always worked as a stand-alone environment, which pleases me. I work in the two environments independently. macports stuff is up-to-date.)

Has anyone else experienced this problem? The test is exceedingly simple:
sage: import matplotlib.pyplot as plt

I actually found the same reports you suggest via Google before submitting this question to sage-support.
What I have attempted:

/Applications/sage/local/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/tkagg.py
changed import _tkagg
to import backend_tkagg as _tkagg
which then caused the error
ImportError: cannot import name backend_tkagg

2. Acting on http://ask.sagemath.org/question/3962/gui-backend-matplotlib-in-sage, I rebuilt matplotlib via sage -f matplotlib
That failed, probably for the reason explained in that report, so I reinstalled vanilla Sage 6.2

If I am the only person experiencing this problem, then it must be something in my configuration,
but I’d really like to know if this is a problem in the Sage 6.2 distribution, so it can get fixed.

Thanks to all,
Jim

On May 27, 2014, at 9:33 AM, kcrisman <kcri...@gmail.com> wrote:

kcrisman

unread,
May 28, 2014, 8:11:55 AM5/28/14
to sage-s...@googlegroups.com, John H. Palmieri, Volker Braun


Thank you for the response, kcrisman (sorry I don’t know your first name).

No problem - very long ago I just chose this handle.
 
Thanks for your config info - I don't see why any of this should affect it, thankfully, though I'm sorry you have had such trouble!  Glad you found the other references; this is puzzling, I agree.  It can't be universal or the doctest I mentioned earlier would be failing.

In fact, one test you could do is to test that one file.  You'd have to cd into the actual Sage-6.2-... directory and then do

./sage -t src/sage/plot/plot.py
 
I think so, at least - the syntax seems to have changed somewhat with the change in the directory layout.

Has anyone else experienced this problem? The test is exceedingly simple:
sage: import matplotlib.pyplot as plt

This is working for me, on 10.7 built from scratch, though; I haven't tried the binary.

Can someone who has access to 10.9 Mavericks download the binary and see if this is a problem?  My suspicion is that something went wrong in the compilation of the binary, though I can't say what.

- kcrisman

Jim Clark

unread,
May 28, 2014, 1:03:32 PM5/28/14
to sage-s...@googlegroups.com
On May 28, 2014, at 5:11 AM, kcrisman <kcri...@gmail.com> wrote:

In fact, one test you could do is to test that one file.  You'd have to cd into the actual Sage-6.2-... directory and then do

./sage -t src/sage/plot/plot.py
 
Fails the test (same error: cannot import name _tkagg):

Jims-computer:sage jim$ ./sage -t src/sage/plot/plot.py
init.sage does not exist ... creating
Running doctests with ID 2014-05-28-09-13-04-3aafce8a.
Doctesting 1 file.
sage -t src/sage/plot/plot.py
**********************************************************************
File "src/sage/plot/plot.py", line 262, in sage.plot.plot
Failed example:
    from pylab import *
… (skipping)
        from matplotlib.backends import _tkagg
    ImportError: cannot import name _tkagg
… (skipping much more)
3 items had failures:
  11 of  77 in sage.plot.plot
   7 of  12 in sage.plot.plot.SelectiveFormatter
   7 of  12 in sage.plot.plot.SelectiveFormatter.None._SelectiveFormatterClass.__init__
    [374 tests, 25 failures, 93.39 s]
----------------------------------------------------------------------
sage -t src/sage/plot/plot.py  # 25 doctests failed
----------------------------------------------------------------------
Total time for all tests: 93.8 seconds
    cpu time: 70.3 seconds
    cumulative wall time: 93.4 seconds

I captured the entire output into a log file that I could send, if that would help.

Still the question remains: does anyone else experience the failure on Mac OSX 10.9 when you do:

sage: import matplotlib.pyplot as plt

Again, thanks,
Jim

John H Palmieri

unread,
May 28, 2014, 1:15:53 PM5/28/14
to sage-s...@googlegroups.com

Try this: start a new Sage session and do

    sage: import matplotlib
    sage: matplotlib.get_backend()

What does it return? If it returns 'TkAgg', try

    sage: matplotlib.use('agg')

    sage: import matplotlib.pyplot as plt

Let us know what happens.

--
John

John H Palmieri

unread,
May 28, 2014, 1:21:44 PM5/28/14
to sage-s...@googlegroups.com

Oh, and to answer your other question: I just downloaded the Sage binary on an OS X Mavericks 10.9.3 machine and was unable to replicate the problem. I wonder if the presence of the macports stuff is interfering somehow. Have you set any environment variables related to matplotllib (other than SAGE_MATPLOTLIB_GUI)?

--
John

Jim Clark

unread,
May 28, 2014, 1:29:56 PM5/28/14
to sage-s...@googlegroups.com
On May 28, 2014, at 10:15 AM, John H Palmieri <jhpalm...@gmail.com> wrote:

Try this: start a new Sage session and do

    sage: import matplotlib
    sage: matplotlib.get_backend()

What does it return? If it returns 'TkAgg', try

    sage: matplotlib.use('agg')
    sage: import matplotlib.pyplot as plt

Let us know what happens.


ims-computer:~ jim$ sage
┌────────────────────────────────────────────────────────────────────┐
│ Sage Version 6.2, Release Date: 2014-05-06                         │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: import matplotlib
sage: matplotlib.get_backend()
'TkAgg'
sage: matplotlib.use('agg')
sage: import matplotlib.pyplot as plt
sage: 


Success! A work-around (albeit an obscure one…)

Jim

Jim Clark

unread,
May 28, 2014, 1:42:36 PM5/28/14
to sage-s...@googlegroups.com
On May 28, 2014, at 10:21 AM, John H Palmieri <jhpalm...@gmail.com> wrote:

Oh, and to answer your other question: I just downloaded the Sage binary on an OS X Mavericks 10.9.3 machine and was unable to replicate the problem. I wonder if the presence of the macports stuff is interfering somehow. Have you set any environment variables related to matplotllib (other than SAGE_MATPLOTLIB_GUI)?

Short answer: no.  Could be PATH or PYTHONPATH ?

Jims-computer:~ jim$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/7f/87pw4jw1691785kvc9kw2lkr0000gv/T/
Apple_PubSub_Socket_Render=/tmp/launch-rgHVzO/Render
TERM_PROGRAM_VERSION=326
TERM_SESSION_ID=3E9FF6BD-7ADF-42F9-A763-BF3EC2E001B2
USER=jim
SAGE_BROWSER=open -a "/Applications/Google Chrome.app"
SSH_AUTH_SOCK=/tmp/launch-9nUl4C/Listeners
__CF_USER_TEXT_ENCODING=0x1FB:0:0
PATH=/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Applications/sage:/usr/local/cuda/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/texbin:/usr/local/bin
__CHECKFIX1436934=1
PWD=/Users/jim
EDITOR=/usr/bin/edit
LANG=en_US.UTF-8
SHLVL=1
HOME=/Users/jim
DYLD_LIBRARY_PATH=/usr/local/cuda/lib:
PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:
LOGNAME=jim
SAGE_MATPLOTLIB_GUI=no
SECURITYSESSIONID=186a5
_=/usr/bin/env


Also…
~/.matplotlib/matplotlibrc contains
backend:  TkAgg

I’ll try moving this out of the way…

Jim

Jim Clark

unread,
May 28, 2014, 1:52:07 PM5/28/14
to sage-s...@googlegroups.com
On May 28, 2014, at 10:42 AM, Jim Clark <jimfort...@earthlink.net> wrote:

Also…
~/.matplotlib/matplotlibrc contains
backend:  TkAgg

I’ll try moving this out of the way…


Found the culprit! Renamed matplotlibrc to matplotlibrc.bak,
started a fresh terminal window and restarted Sage
and now import matplotlib.pyplot as plt works.

Thanks, John Palmieri — your suggestion led me to the source of the problem.

Perhaps I need to find a better way to insulate macports from Sage…

Jim

John H Palmieri

unread,
May 28, 2014, 2:26:36 PM5/28/14
to sage-s...@googlegroups.com


It looks like the actual problem is a bug in Sage (which has been fixed in the beta releases of Sage 6.3). Namely, when Sage runs, it is supposed to use ~/.sage/matplotlib-1.3.1 rather than ~/.matplotlib for matplotlib configuration (just for your sort of situation), but this doesn't work right in Sage 6.2. This is fixed at http://trac.sagemath.org/ticket/16305, in case you want to make the change to your copy of Sage 6.2.

--
John

Jim Clark

unread,
May 28, 2014, 2:55:21 PM5/28/14
to sage-s...@googlegroups.com
On May 28, 2014, at 11:26 AM, John H Palmieri <jhpalm...@gmail.com> wrote:

It looks like the actual problem is a bug in Sage (which has been fixed in the beta releases of Sage 6.3). Namely, when Sage runs, it is supposed to use ~/.sage/matplotlib-1.3.1 rather than ~/.matplotlib for matplotlib configuration (just for your sort of situation), but this doesn't work right in Sage 6.2. This is fixed athttp://trac.sagemath.org/ticket/16305, in case you want to make the change to your copy of Sage 6.2.

Ahh, so it is a bug.. I’m glad it’s been found and fixed. I’ll wait for Sage 6.3, since I now have a workaround.
Thanks again for your help.
Jim

kcrisman

unread,
May 28, 2014, 4:35:48 PM5/28/14
to sage-s...@googlegroups.com
It looks like the actual problem is a bug in Sage (which has been fixed in the beta releases of Sage 6.3). Namely, when Sage runs, it is supposed to use ~/.sage/matplotlib-1.3.1 rather than ~/.matplotlib for matplotlib configuration (just for your sort of situation), but this doesn't work right in Sage 6.2. This is fixed athttp://trac.sagemath.org/ticket/16305, in case you want to make the change to your copy of Sage 6.2.

Ahh, so it is a bug.. I’m glad it’s been found and fixed. I’ll wait for Sage 6.3, since I now have a workaround.
Thanks again for your help.
Jim

Awesome resolution.  Thanks for helping on this, John.  I don't know why the rc file didn't occur to me, that often has been a problem in the past, as you say.

- kcrisman
Reply all
Reply to author
Forward
0 new messages