3

I am on CentOs7. I installed tk, tk-devel, tkinter through yum. I can import tkinter in Python 3, but not in Python 2.7. Any ideas?

Success in Python 3 (Anaconda):

Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

But fail on Python 2.7 (CentOS default):

Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 39, in <module>
  import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libTix.so: cannot open shared object file: No such file or directory

I read some answers said

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

I have reinstalled tk, tk-devel and tkinter through yum, but the problem is same.

How can I configure it to work on Python 2.7?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
xiaosj
  • 31
  • 3
  • 1
    This is because you installed the Python3 modules of each. Now, install for Python2 again. – Aakash Verma Dec 26 '17 at 00:01
  • You should install it for Python2. – Mike Ru Dec 26 '17 at 00:05
  • Thank you for the answer. How can I install it for Python2 while not for Phyton3. I do have the files in `/usr/lib64/python2.7/lib-tk` and `/usr/lib64/python2.7/lib-dynload` @MikeRu @AakashVerma – xiaosj Dec 26 '17 at 00:20
  • sudo yum install tkinter. Not ? – Mike Ru Dec 26 '17 at 00:36
  • On Linux Mint I use `apt` which has `python-tk`, `python2-tk`, `python2.7-tk`, `python3-tk`, `python3.6-tk`, etc. But I didn't have to install it because I got preinstalled `tkinter` in both Python. But maybe you have to copy `.so` files to other version - maybe it will work if it is `tk` library, not wrapper for Python. – furas Dec 26 '17 at 00:57
  • I was using `yum install tkinter`. But it did not help. – xiaosj Dec 26 '17 at 01:31
  • Does this answer your question? [Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library?](https://stackoverflow.com/questions/76105218/why-does-tkinter-or-turtle-seem-to-be-missing-or-broken-shouldnt-it-be-part) – Karl Knechtel Apr 30 '23 at 00:07

2 Answers2

0

For python 3 use:

import tkinter

For python 2 use:

import Tkinter

If these do not work install with, for python 3:

sudo apt-get install python3-tk

or, for python 2:

sudo apt-get install python-tk

you can find more details here

Alfonso
  • 644
  • 7
  • 17
-1

For python2.7 try

import Tkinter

With a capital T. It should already be pre-installed in default centos 7 python setup, if not do yum install tkinter

harpratap
  • 365
  • 6
  • 12