Tkinter: "Python may not be configured for Tk"

PythonPython 3.xTkinter

Python Problem Overview


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 can I configure my Python 3.2 to work with Tkinter?

Python Solutions


Solution 1 - Python

Under Arch/Manjaro just install the package tk:

sudo pacman -S tk

Solution 2 - Python

Solution for MacOS

After trying a bunch of things, this is how it finally worked:

$ brew install python-tk

Solution 3 - Python

Install tk-devel (or a similarly-named package) before building Python.

Solution 4 - Python

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

> 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.

Solution 5 - Python

To get this to work with pyenv on Ubuntu 16.04 and 18.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

Solution 6 - Python

Had the same issue on Fedora with Python 2.7. Turns out some extra packages are required:

sudo dnf install tk-devel tkinter

After installing the packages, this hello-world example seems to be working fine on 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 X11 forwarding, it looks like this:

Hello World through X11

Note that in Python 3, the module name is lowercase, and other packages are probably required...

from tkinter import *

Solution 7 - Python

I also faced similar problem. I resolved it by installing python-tk in my system.

Command for mac : brew install python-tk.

Solution 8 - Python

Oh I just have followed the solution Ignacio Vazquez-Abrams has suggest which is install tk-dev before building the python. (Building the Python-3.6.1 from source on Ubuntu 16.04.)

There was pre-compiled objects and binaries I have had build yesterday though, I didn't clean up the objects and just build again on the same build path. And it works beautifully.

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

That's it!

Solution 9 - Python

sudo apt-get install python3-tk

Solution 10 - Python

So appearantly many seems to have had this issue (me including) and I found the fault to be that Tkinter wasn't installed on my system when python was compiled.

This post describes how to solve the problem by:

  1. Removing the virtual environment/python distribution
  2. install Tkinter with sudo apt-get install tk-dev (for deb) or sudo pacman -S tk (for arch/manjaro)
  3. Then proceed to compile python again.

This worked wonders for me.

Solution 11 - Python

I encountered this issue on python 2.7.9.
To fix it, I installed tk and tcl, and then rebuild python code and reinstall, and during configure, I set the path for tk and tcl explicitly, by:

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

Also, a whole article for python install process: Building Python from Source

Solution 12 - Python

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

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

> 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.

Solution 13 - Python

To anyone using Windows and Windows Subsystem for Linux, make sure that when you run the python command from the command line, it's not accidentally running the python installation from WSL! This gave me quite a headache just now. A quick check you can do for this is just
which <python command you're using>
If that prints something like /usr/bin/python2 even though you're in powershell, that's probably what's going on.

Solution 14 - Python

now i figured out what's going on ubuntu,

Follow these step to solve the issue

  1. check your python version python3 --version
  2. Lets Imagine you have python 3.10
  3. Then Install Python-tk for the python version by using bellow command
  4. sudo apt install python3.10-tk

simple if you have python3.8 then sudo apt install python{"use your python version here"}-tk

Solution 15 - Python

This symptom can also occur when a later version of python (2.7.13, for example) has been installed in /usr/local/bin "alongside of" the release python version, and then a subsequent operating system upgrade (say, Ubuntu 12.04 --> Ubuntu 14.04) fails to remove the updated python there.

To fix that imcompatibility, one must

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

b) uninstall python-idle2.7; and

c) reinstall python-idle2.7.

Solution 16 - Python

If you're running on an AWS instance that is running Amazon Linux OS, the magic command to fix this for me was

sudo yum install tkinter

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

Solution 17 - Python

Even after installing python-tk, python3-tk I was getting error your python is not configured for Tk.

So I additionally installed tk8.6-dev Then I build my Python again, run following again: make, make install.

When I did this I saw messages on screen that it is building _tkinter and related modules. Once that is done, I tried 'import tkinter" and it worked.

Solution 18 - Python

Installing Tkinter


python -m pip install tk-tools
or
sudo apt install python3-tk


Code

from tkinter import *

root = Tk()
root.title('My App')
# Code
root.mainloop()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaciej ZiarkoView Question on Stackoverflow
Solution 1 - PythonJabbaView Answer on Stackoverflow
Solution 2 - PythonsubtleseekerView Answer on Stackoverflow
Solution 3 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 4 - PythonSandro MundaView Answer on Stackoverflow
Solution 5 - PythonPaulMestView Answer on Stackoverflow
Solution 6 - PythonStefan SaruView Answer on Stackoverflow
Solution 7 - PythonVikas GuptaView Answer on Stackoverflow
Solution 8 - PythonDavid JungView Answer on Stackoverflow
Solution 9 - PythonRafalView Answer on Stackoverflow
Solution 10 - PythonJakob Guldberg AaesView Answer on Stackoverflow
Solution 11 - PythondasonsView Answer on Stackoverflow
Solution 12 - PythonmjpView Answer on Stackoverflow
Solution 13 - PythonbbukatyView Answer on Stackoverflow
Solution 14 - PythonAnshu MeenaView Answer on Stackoverflow
Solution 15 - Pythons.w.s.View Answer on Stackoverflow
Solution 16 - PythonStackGView Answer on Stackoverflow
Solution 17 - PythonRajendra JasathyView Answer on Stackoverflow
Solution 18 - PythonMr.Programmer nopeView Answer on Stackoverflow