Ask Your Question

sage-python, import matplotlib: no module named _tkagg

asked 2011-06-28 09:41:40 -0600

tdstephens3 gravatar image

updated 2011-06-28 10:44:09 -0600

EDIT 2: Below is the description of a problem and a subsequent edit, where I thought I had the solution. In fact, what I did was move the problem from one place to another... I am no longer receiving the error No module named _tkagg and I am getting a plot window, but after opening a tk window I get a stack trace that reads:

Exception in Tkinter callback
Traceback (most recent call last):
File "/opt/sage/local/lib/python2.6/lib-tk/", line 1410, in __call__
return self.func(*args)
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/", line 245, in resize
File "/opt/sage/local/lib/python2.6/site-
packages/matplotlib/backends/", line 249, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/", 
line 18, in blit
_tkagg.tkinit(id(tk), 0)
AttributeError: 'module' object has no attribute 'tkinit'

along with no actual plot on the tk window. This issue remains unresolved.

I am using Sage's python and trying to import matplotlib.pyplot in order to generate some visual output (to the screen for now, to a file at some point). I understand the backend I should use is TkAgg for this, and I have that set in my matplotlibrc file.

When running a short script


import matplotlib.pyplot as plt 
def main():
    x = [0,1,2,3,4,5]
    y = [1,6,4,4,2,7]


bash$ python

I am greeted with absolutely no output. When examining this closer, by running a sage-python (python2.6.4.p10) shell I get the following:

>>>import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/", line 95,          
in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/opt/sage/local/lib/python2.6/site-    
packages/matplotlib/backends/", line 25, in pylab_setup
File "/opt/sage/local/lib/python2.6/site-    
packages/matplotlib/backends/", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "/opt/sage/local/lib/python2.6/site-packages/matplotlib/backends/",    
line 1, in <module>
import _tkagg
ImportError: No module named _tkagg

I have tried

sage -f python-2.6.4.p10
sage -f matplotlib-1.0.1

but this does not magically fix the problem. As well, Google seems to not be much help. What do you think?

EDIT 1: FIXED! I am able to get a plot window from either the sage-python prompt -or- by running this 'program' from the bash shell. What I did was to track down the _tkagg call (by reading the stack trace more carefully) coming from the file


the file starts out as:

import _tkagg
yada yada yada

so I went searching around ... (more)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2011-07-04 13:39:17 -0600

Jason Grout gravatar image

updated 2011-07-04 13:41:50 -0600

For Sage's matplotlib, the graphical guis are also turned off by default. Define the environment variable:


and then reinstall Sage's matplotlib:

sage -i matplotlib

to compile support for other backends in Sage.

edit flag offensive delete link more


Ah, it looks like you moved on from my answer. Can you post your solution as an "answer" so that people know the question is resolved?

Jason Grout gravatar imageJason Grout ( 2011-07-04 13:40:45 -0600 )edit

answered 2011-08-16 11:07:38 -0600

Alex gravatar image


I have got the same problem after compiling SAGE on my notebook. I searched in the web and I found that the file is missed in the folder /sage-4.7/local/lib/python2.6/site-packages/matplotlib/backends (if I understood well, it was a bug of matplotlib). You can find this file at I use Ubuntu 11.04, there is no file specific for it, so I decided to try with the file for Fedora 14 for i386. I copied it in the folder above and it looks to work well (I tried a couple of graphs).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools


Asked: 2011-06-28 09:41:40 -0600

Seen: 3,008 times

Last updated: Aug 16 '11