Reputation: 12084
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?
Upvotes: 189
Views: 331541
Reputation: 5154
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.
Upvotes: 10
Reputation: 1070
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
.
Upvotes: 0
Reputation: 1409
If you are using Manjaro(Arch Linux) run below command in your terminal
sudo pacman -S tk
Upvotes: 2
Reputation: 183
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
Upvotes: 12
Reputation: 5243
After trying a bunch of things, this is how it finally worked:
$ brew install python-tk
Upvotes: 170
Reputation: 381
now i figured out what's going on ubuntu,
Follow these step to solve the issue
python3 --version
sudo apt install python3.10-tk
simple if you have python3.8 then sudo apt install python{"use your python version here"}-tk
Upvotes: 3
Reputation: 201
python -m pip install tk-tools
sudo apt install python3-tk
from tkinter import *
root = Tk()
root.title('My App')
# Code
root.mainloop()
Upvotes: 3
Reputation: 10904
I also faced similar problem. I resolved it by installing python-tk
in my system.
Command for mac : brew install python-tk
.
Upvotes: 6
Reputation: 1
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.
Upvotes: 0
Reputation: 844
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:
sudo apt-get install tk-dev
(for deb) or sudo pacman -S tk
(for arch/manjaro)This worked wonders for me.
Upvotes: 10
Reputation: 14985
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
Upvotes: 67
Reputation: 2858
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
Upvotes: 1
Reputation: 501
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
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 406
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!
Upvotes: 4
Reputation: 11
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.
Upvotes: 0
Reputation: 1679
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.
Upvotes: 1
Reputation: 20624
Under Arch/Manjaro just install the package tk
:
sudo pacman -S tk
Upvotes: 134
Reputation: 1789
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:
Note that in Python 3, the module name is lowercase, and other packages are probably required...
from tkinter import *
Upvotes: 4
Reputation: 41030
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.
Upvotes: 39
Reputation: 798536
Install tk-devel
(or a similarly-named package) before building Python.
Upvotes: 68