154

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?

Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
  • 1
    Although this doesn't directly answer your question, if you're looking to use the latest Python on Linux, using pre-compiled binaries (such as ActivePython - which includes Tkinter) would save a lot of time. – Sridhar Ratnakumar Mar 28 '11 at 16:46
  • 2
    Every Linux distribution ever has an official python package. Unless you need something specific, using 3rd party binaries is actually discouraged. That said, you are probably one or two commands from having Tk support in python, but I need to know what flavor of Linux you are running. – Mad Wombat Jan 12 '16 at 15:48
  • I think the most complete answer to this is here: [How to get tkinter working with Ubuntu's default Python 2.7 install?](http://stackoverflow.com/a/11799470/5402386) – mjp Sep 09 '16 at 20:10

22 Answers22

130

Solution for Linux, Windows (WSL/Ubuntu) and MacOS

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

$ brew install python-tk
rainabba
  • 3,804
  • 35
  • 35
subtleseeker
  • 4,415
  • 5
  • 29
  • 41
  • 8
    The brew install worked for me on Catalina – Pranab May 18 '21 at 07:01
  • 3
    The pip3 package `tk` is TensorKit, see [here](https://pypi.org/project/tk/). I don't think this helps. – Patrick Aug 04 '21 at 12:43
  • 1
    brew install from my macOS terminal did the work, once I got back to Pycharm the 'no module' error was gone. – brunoazev Sep 22 '21 at 00:24
  • Good lord, thanks. I was trying a lot of things and finally, this made it work. – Naseem Ahamed Jan 21 '22 at 20:59
  • 3
    Brew isn't a "MacOS" thing. It's supported by Linux (including in WSL/Windows 10) as well as MacOS. In fact, the Linuxbrew project has been merged entirely into HomeBrew, so yeah; definately not a "MacOS" thing. – rainabba May 24 '22 at 23:48
  • 1
    @rainabba but no self respecting linux user would use brew when there's asdf or the package manager available for the OS.... i mean you can't even pin a package to a version with brew.... – airtonix Apr 10 '23 at 07:23
  • every time I use brew install I want to get a hammer and disassemble the laptop. I can't believe that it always downloads 300 other things along the way, and `pouring` forever :rofl: I mean, why in heavens name would it download `php` if you want to get python-tk??? –  Jun 09 '23 at 04:52
121

Under Arch/Manjaro just install the package tk:

sudo pacman -S tk
Jabba
  • 19,598
  • 6
  • 52
  • 45
58

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 37
    On Ubuntu run 'sudo apt-get install tk-dev', and then re-run make – pycoder112358 Jan 16 '12 at 22:01
  • 1
    @pycoder112358: I did it but it's still telling me `ImportError: No module named _tkinter` when I try to import tkinter. I use python3.4.0 compiled from source on ubuntu 13.10. – ARF Apr 04 '14 at 22:02
  • 10
    What's `tk-devel` ? You should give at least a minimal explanation of why it should be useful in this case. – nbro Aug 08 '15 at 09:20
  • 8
    As a comment to others, the "before building Python" is important here. When you install the module you need to rebuild python before it'll find the module. – mjp Sep 09 '16 at 20:09
  • This is the correct solution when installing Python from source on a Linux system. – filip Jul 19 '23 at 07:43
51

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
PaulMest
  • 12,925
  • 7
  • 53
  • 50
37

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.

Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
  • 4
    How do you edit the setup.py script? How do you rerun make? I'm trying to do this on a mac and I'm finding that I should just return the mac and get a windows machine. – Drew Rush Feb 16 '14 at 09:04
  • 4
    In my case this was caused by an upgrade of `tk`. It was only used by `matplotlib`, and that can be circumnavigated: http://stackoverflow.com/a/4935945/1959808 – 0 _ Nov 18 '15 at 06:31
  • 1
    the setup.py is located in source code directory of python – dasons Sep 08 '17 at 13:47
  • 3
    I have roughly 50 setup.py files in my computer, which is the right one? – logic1976 Oct 19 '19 at 02:36
  • Simply installing the `tk` package fixed the problem for me. – n-anselm Feb 05 '22 at 16:50
8

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.

  • 1
    This is the right solution, thanks! I still don't know why the most upvoted answers make you go through a lot of hoops without explaining how to do it even and a simple command line is buried at the bottom... – tralph3 Sep 25 '20 at 18:42
  • Thank you for your kind words. Hope that someone else might find this more useful than all the guesswork that has been done is this thread :/ – Jakob Guldberg Aaes Sep 28 '20 at 07:38
6

since I can not comment yet, here's my answer to another post:

since I'm still using python 3.9, this code works for me:

brew install python-tk@3.9

if using brew install python-tk brew will install python-tk@3.10 which is key-only

Ann
  • 103
  • 2
  • 4
5

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

Command for mac : brew install python-tk.

Vikas Gupta
  • 10,779
  • 4
  • 35
  • 42
4

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 *
Stefan Saru
  • 1,731
  • 2
  • 10
  • 4
4

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!

David Jung
  • 376
  • 5
  • 8
3
sudo apt-get install python3-tk
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
Rafal
  • 573
  • 5
  • 11
3

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()
3

if you're managing python with https://asdf-vm.com/ on linux, then :

Fedora

$ asdf uninstall python 3.10.10
$ sudo dnf install tk-devel
$ asdf install  python 3.10.10

Ubuntu

$ asdf uninstall python 3.10.10
$ sudo apt-get install tk-dev
$ asdf install  python 3.10.10
airtonix
  • 4,772
  • 2
  • 38
  • 36
2

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

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
dasons
  • 493
  • 5
  • 12
2

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

Anshu Meena
  • 169
  • 1
  • 6
  • `apt install python3.7-tk` Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package python3.7-tk E: Couldn't find any package by glob 'python3.7-tk' E: Couldn't find any package by regex 'python3.7-tk' – Cedric Martens May 09 '23 at 18:29
1

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.

Community
  • 1
  • 1
mjp
  • 1,618
  • 2
  • 22
  • 37
1

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.

bbukaty
  • 11
  • 1
1

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

StackG
  • 2,730
  • 5
  • 29
  • 45
1

If you are using Manjaro(Arch Linux) run below command in your terminal

sudo pacman -S tk
Bushra Mustofa
  • 1,075
  • 7
  • 12
0

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.

s.w.s.
  • 11
  • 2
0

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.

0

In my case, pyenv was installed but I was not aware of it. I needed to uninstall and reinstall the python version via pyenv to fix the tkinter configuration after installing the right version of python3.x-tk.

Christoph Thiede
  • 707
  • 10
  • 16