Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get tkinter working with Ubuntu's default Python 2.7 install?

I'm using Ubuntu 11.10, which came pre-installed with Python 2.7.3.

I installed the python3 package, and the python3-tk package, via apt-get install, and they worked together "out of the box".

But though I've installed the python-tk package, I can't figure out how to get Python2.7 to see it. I get the error message below when I try to import it.

import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

edit: I also ran the following based on Python-tk package not recognized in Python 2.7.3, and got:

$ file /usr/lib/libtk8.5.so.0
/usr/lib/libtk8.5.so.0: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped

$ uname -a
Linux bugbot 3.0.0-23-generic-pae #39-Ubuntu SMP Thu Jul 19 19:39:19 UTC 2012 i686 i686 i386 GNU/Linux
like image 564
ellriley Avatar asked Dec 09 '22 22:12

ellriley


1 Answers

I figured it out after way too much time spent on this problem, so hopefully I can save someone else the hassle.

I found this old bug report deemed invalid that mentioned the exact problem I was having, I had Tkinter.py, but it couldn't find the module _tkinter: http://bugs.python.org/issue8555

I installed the tk-dev package with apt-get, and rebuilt Python using ./configure, make, and make install in the Python2.7.3 directory. And now my Python2.7 can import Tkinter, yay!

I'm a little miffed that the tk-dev package isn't mentioned at all in the Python installation documentation.... below is another helpful resource on missing modules in Python if, like me, someone should discover they are missing more than _tkinter.

Building Python and more on missing modules

like image 171
ellriley Avatar answered Dec 11 '22 12:12

ellriley