7

When I run the following code:

import Tkinter

I get this error message:

>>> import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: /usr/lib/libtk8.5.so.0: invalid ELF header, please install the python-tk package

however, python-tk is installed.

$ sudo apt-get install python-tk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-tk is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Looking into why this is failing, I looked at Tkinter.py. This is the line that is failing.

try:
    import _tkinter
except ImportError, msg:
    raise ImportError, str(msg) + ', please install the python-tk package'

Is this a path problem? Any help would be much appreciated.

Michael
  • 305
  • 1
  • 4
  • 12

2 Answers2

6

It seems like maybe that library is corrupted. Try sudo apt-get remove python-tk, then sudo apt-get clean so that you will re-download the package, sudo apt-get install python-tk, and then try importing again. Another possibility is that you've somehow got your apt/sources.list messed up, and you've installed a library that is for the wrong platform.

If it is still not working after you try the above, leave a comment on this answer.

bstpierre
  • 28,439
  • 14
  • 66
  • 101
  • Did not work. I looked at the sources.list, looks normal to me (not an expert!) – Michael Jun 15 '12 at 03:03
  • If you run `file /usr/lib/libtk8.5.so.0` and `uname -a`, what do they report? – bstpierre Jun 15 '12 at 11:50
  • problem solved. Thanks for the input though. – Michael Jun 15 '12 at 18:26
  • Hey, sorry to piggy-back on an old question but I am having the exact same problem: http://stackoverflow.com/questions/11752174/how-to-get-tkinter-working-with-ubuntus-default-python-2-7-install I did the run what you suggested in your comment, here's the results: erika@bugbot:~$ 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 erika@bugbot:~$ 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 – ellriley Aug 01 '12 at 03:25
  • plt.imshow() works for me but when I'm in a virtual environment, it doesn't work. (it used to work before). What should I do? – Chan Kim Nov 07 '19 at 06:07
  • @ChanKim you should ask that in a separate question. – bstpierre Nov 07 '19 at 19:22
2

I'm not sure what the problem was, but it's been resolved. My VM got corrupted during a VMware Player update, so I had to reinstall. After the fresh install, things are working as they should. Thanks for the help.

Michael
  • 305
  • 1
  • 4
  • 12