Tkinter: "Python cannot be configured for Tk"

Today I wanted to start working with Tkinter, but I have some problems.

Python 3.2 (r32:88445, Mar 28 2011, 04:14:07) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from tkinter import * Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in <module> import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named _tkinter 

So, how do I configure Python 3.2 to work with Tkinter?

+64
python tkinter
Mar 28 '11 at 13:10
source share
13 answers

According to http://wiki.python.org/moin/TkInter :

If it does not work with "No module named _tkinter", your Python configuration must be changed to include this module (which is an extension module implemented in C). Do not edit modules / settings (deprecated). You may need to install Tcl and Tk (if using RPM, also install RPM -devel) and / or edit setup.py script to indicate where you need Tcl / Tk installed. If you install Tcl / Tk in the default locations, just restarting "make" should build the _tkinter extension.

+27
Mar 28 '11 at 13:15
source share

In Arch / Manjaro just install the tk package:

 sudo pacman -S tk 
+67
Apr 27 '16 at 21:27
source share

Install tk-devel (or a package with a similar name) before creating Python.

+42
Mar 28 '11 at 13:14
source share

To work with pyenv on Ubuntu 16.04, I had to:

 $ sudo apt-get install python-tk python3-tk tk-dev 

Then install the version of Python I wanted:

 $ pyenv install 3.6.2 

Then I could import tkinter just fine:

 import tkinter 
+14
Aug 10 '17 at 19:42 on
source share

Had the same issue with Fedora with Python 2.7. It turns out that additional packages are required:

 sudo dnf install tk-devel tkinter 

After installing the packages, this hi-world seems to work fine in Python 2.7:

 $ cat hello.py from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop() $ python --version Python 2.7.8 $ python hello.py 

And through the X11 forwarding, it looks like this:

Hello World through X11

Note that in Python 3, the module name has string values, and other packages may be needed ...

 from tkinter import * 
+3
Jan 12 '16 at 15:39
source share

I ran into this problem on Python 2.7.9.
To fix this, I installed tk and tcl, and then rebuilt the code in python and reinstalled, and during the setup, I explicitly set the path for tk and tcl:

 ./configure --with-tcltk-includes="-I/usr/include" --with-tcltk-libs="-L/usr/lib64 -ltcl8.5 -L/usr/lib64 -ltk8.5" 

Also, an entire article on the Python installation process: Building Python from Source

+2
08 Sep '17 at 14:00
source share

Oh, I just followed the solution that Ignacio Vasquez-Abrams offers to install tk-dev before creating python. (Building Python-3.6.1 from a source on Ubuntu 16.04.)

There were pre-compiled objects and binaries that I had yesterday, but I did not clear the objects and just built them again on the same build path. And it works great.

 sudo apt install tk-dev (On the python build path) (No need to conduct 'make clean') ./configure make sudo make install 

What is it!

+1
Jun 22 '17 at 0:42
source share
 sudo apt-get install python3-tk 
+1
Jul 23 '17 at 9:32
source share

Anyone using Windows and the Windows Subsystem for Linux should make sure that when they run the python command from the command line, it is no coincidence that they start the python installation from WSL! It gave me a headache just now. The quick check you can do is just
which <python command you're using>
If this prints something like /usr/bin/python2 even if you're in PowerShell, this is probably what happens.

+1
May 22 '18 at 5:47
source share

I think the most complete answer to this question is the accepted answer found here:

How to make tkinter work with Ubuntu by default Python 2.7 install?

I realized this after too much time spent on this problem, so I hope I can save someone else from the hassle.

I found that this old error report was invalidated, I had a problem, I had Tkinter.py, but he could not find the _tkinter module: 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 also now my Python2.7 can import Tkinter, yay!

I'm a little surprised that the tk-dev package is not mentioned at all in the Python installation documentation .... below is another useful resource for missing modules in Python, if, like me, someone should find that they are missing more, than _tkinter.

0
Sep 09 '16 at 20:12
source share

This symptom can also occur when a later version of python (e.g. 2.7.13) was installed in / usr / local / bin "along with the" python release version "and then a subsequent update to the operating system (say, Ubuntu 12.04 → Ubuntu 14.04) does not remove updated python there.

To fix this incompatibility, you need

a) delete the updated python version in / usr / local / bin;

b) remove python-idle2.7; and

c) reinstall python-idle2.7.

0
Jun 13 '17 at 0:50
source share

If you are working with an AWS instance running Amazon Linux, the magic team that fixed it for me was

 sudo yum install tkinter 

If you want to define your Linux build, try cat/etc/*release

0
Nov 14 '18 at 4:52
source share

You need to install tkinter for python3.

In Fedora pip3 install tkinter --user returns Could not find a version that satisfies the requirement ... so I have to dnf install python3-tkinter command : dnf install python3-tkinter . It solved my problem

-one
Feb 06 '19 at 20:46
source share



All Articles