Tkinter: "Python may not be configured for Tk" [duplicate]
Asked Answered
L

22

168

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?

Louth answered 28/3, 2011 at 13:10 Comment(3)
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.Gracious
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.Drub
I think the most complete answer to this is here: How to get tkinter working with Ubuntu's default Python 2.7 install?Little
C
38

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.

Choreograph answered 28/3, 2011 at 13:15 Comment(5)
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.Betulaceous
In my case this was caused by an upgrade of tk. It was only used by matplotlib, and that can be circumnavigated: https://mcmap.net/q/100487/-generating-matplotlib-graphs-without-a-running-x-server-duplicateCrystallography
the setup.py is located in source code directory of pythonHomemaker
I have roughly 50 setup.py files in my computer, which is the right one?Resolvent
Simply installing the tk package fixed the problem for me.Diazole
D
149

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

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

$ brew install python-tk
Diaspora answered 10/5, 2021 at 6:27 Comment(8)
The brew install worked for me on CatalinaCreaky
The pip3 package tk is TensorKit, see here. I don't think this helps.Justus
brew install from my macOS terminal did the work, once I got back to Pycharm the 'no module' error was gone.Zoellick
Good lord, thanks. I was trying a lot of things and finally, this made it work.Bludge
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.Danieu
@Danieu 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....Countrified
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???Clothesbasket
You can also use something like brew install [email protected] if you require a specific version of pythonPlasia
B
129

Under Arch/Manjaro just install the package tk:

sudo pacman -S tk
Baba answered 27/4, 2016 at 21:27 Comment(0)
F
62

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

Falbala answered 28/3, 2011 at 13:14 Comment(7)
On Ubuntu run 'sudo apt-get install tk-dev', and then re-run makeLederer
@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.Soninlaw
What's tk-devel ? You should give at least a minimal explanation of why it should be useful in this case.Redmon
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.Little
This is the correct solution when installing Python from source on a Linux system.Palingenesis
I am installing Python via asdf, and I have installed tk-dev on Ubuntu, and I am still getting this error.Housewife
I deleted my python 3.12.0, installed tk-dev, downloaded python 3.12.2 and then built from source 3.12.2 and I still cannot import tkinter (ubuntu 18.04).Tartrazine
G
56

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
Genista answered 10/8, 2017 at 19:42 Comment(3)
And for MacOS X all you need is brew install python-tk.Thermoelectrometer
Intuitively I'd think that you would only need to install the tk-dev package for this to work, but I haven't tested it so I'm not sure.Hayton
@AviNerenberg your intuition was right. I had to install only tk-dev to make it work.Subdeacon
C
38

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.

Choreograph answered 28/3, 2011 at 13:15 Comment(5)
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.Betulaceous
In my case this was caused by an upgrade of tk. It was only used by matplotlib, and that can be circumnavigated: https://mcmap.net/q/100487/-generating-matplotlib-graphs-without-a-running-x-server-duplicateCrystallography
the setup.py is located in source code directory of pythonHomemaker
I have roughly 50 setup.py files in my computer, which is the right one?Resolvent
Simply installing the tk package fixed the problem for me.Diazole
D
9

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.

Deb answered 10/3, 2020 at 6:23 Comment(2)
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...Gouache
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 :/Deb
C
7

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 [email protected]

if using brew install python-tk brew will install [email protected] which is key-only

Cassady answered 11/9, 2022 at 10:24 Comment(1)
this finally worked!Bearable
W
5

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

Command for mac : brew install python-tk.

Wellheeled answered 10/5, 2021 at 15:51 Comment(0)
C
5

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

Macos

$ asdf uninstall python 3.10.10
$ brew install python-tk
$ asdf install  python 3.10.10

This is because installing python with asdf will cause it to compile parts of the python core. If certain things aren't present at the time then they don't get included/enabled. So you need to make those things present and remove and install python again.

Countrified answered 10/4, 2023 at 22:37 Comment(2)
This is what worked for me on macOS as I was also managing my pythons via asdf. Thank you!Harbourage
This is what fixed my issue on Ubuntu. Thank you.Carbonari
H
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 *
Heavenward answered 12/1, 2016 at 15:39 Comment(1)
sudo: dnf: command not found (I'm ubuntu, is this the difference?)Little
M
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!

Mucilaginous answered 22/6, 2017 at 0:42 Comment(0)
P
3
sudo apt-get install python3-tk
Phenanthrene answered 23/7, 2017 at 9:32 Comment(0)
G
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()
Galloromance answered 26/9, 2021 at 11:42 Comment(0)
H
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

Homemaker answered 8/9, 2017 at 14:0 Comment(0)
M
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

Montano answered 14/12, 2021 at 1:3 Comment(1)
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'Senter
L
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.

Little answered 9/9, 2016 at 20:12 Comment(0)
T
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.

Theomania answered 22/5, 2018 at 5:47 Comment(0)
J
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

Jair answered 14/11, 2018 at 4:52 Comment(0)
U
1

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

sudo pacman -S tk
Uyekawa answered 12/10, 2022 at 8:1 Comment(0)
A
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.

Adenitis answered 13/6, 2017 at 0:50 Comment(0)
O
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.

Orelu answered 25/9, 2020 at 12:47 Comment(0)
O
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.

Occultation answered 28/3, 2023 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.