4

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
Community
  • 1
  • 1
ellriley
  • 605
  • 2
  • 7
  • 21
  • I know that in Python 3, the Tkinter module is lowercase ("tkinter"). so you could try "import tkinter" instead. Then again, this person (http://ubuntuforums.org/showthread.php?t=1699456) seems to have had a similar problem, but it looks like it ran deeper than just using the wrong name. – seaotternerd Aug 01 '12 at 03:47
  • I've tried both lowercase and capitalized...neither worked, only capitalized got me the more specific error message. – ellriley Aug 01 '12 at 03:58

3 Answers3

12

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

Community
  • 1
  • 1
ellriley
  • 605
  • 2
  • 7
  • 21
  • if you compile python under fedora do `sudo yum install tk-devel` before `./configure` – User Jan 07 '13 at 13:04
  • "I'm a little miffed that the tk-dev package isn't mentioned at all in the Python installation documentation" That's because it's specifically intended for building Python from scratch. The `python-tk` and `python3-tk` packages are meant for upgrading a system Python in place (ideally we don't try to rebuild the system Python). If the latter don't work for that purpose, that's a bug to file with your vendor, not against Python. – Karl Knechtel Apr 29 '23 at 02:06
2

I had same issue with python3.4 (ImportError: No module named '_tkinter')

sudo apt-get install tk-dev

got to python source directory

./configure
make
sudo make install
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

The following solved the issue for me on Linux Mint 16:

sudo apt-get install tk-dev python-tk 

I use virtualenv but did not want to compile python, in order to make the python support tk, python-tk should be installed.

Oleksandr
  • 607
  • 6
  • 7