412

For some reason, I can't use the Tkinter (or tkinter, on Python 3) module. After running the following command in the python shell:

import Tkinter

or this, in Python 3:

import tkinter

I got this error

ModuleNotFoundError: No module named 'Tkinter'

or this:

ModuleNotFoundError: No module named 'tkinter'

What could be the reason for these errors and how can I solve it?

Ben the Coder
  • 539
  • 2
  • 5
  • 21
RasmusGP
  • 4,696
  • 5
  • 21
  • 28

28 Answers28

624

You probably need to install it using something similar to the following:

  • For Ubuntu or other distros with Apt:

    sudo apt-get install python3-tk
    
  • For Fedora:

    sudo dnf install python3-tkinter
    

You can also mention a Python version number like this:

  • sudo apt-get install python3.7-tk
    
  • sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
    

Finally, import tkinter (for Python 3) or Tkinter (for Python 2), or choose at runtime based on the version number of the Python interpreter (for compatibility with both):

import sys
if sys.version_info[0] == 3:
    import tkinter as tk
else:
    import Tkinter as tk
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
d-coder
  • 12,813
  • 4
  • 26
  • 36
  • 4
    It was the change in capitalization (Tkinter to tkinter) that got me - everyone now needs to update all of their sample code :) – Guy Starbuck Oct 16 '19 at 14:13
  • 6
    Why does it need to be installed if it is a [standard python interface](https://en.wikipedia.org/wiki/Tkinter)? – Marko Jan 25 '20 at 15:35
  • 37
    and for macOS, `brew install python-tk` – Erik Kaplun Feb 05 '22 at 12:38
  • 2
    finally for Windows and win32, remember to check the `Tcl support for Python 3.11` when installing. – Misinahaiya Dec 21 '22 at 11:47
  • MacOS - if using MacPorts rather than Homebrew, something along the lines of the following: `python --version; port search python | grep tk` followed by the like of: `sudo port install py311-tkinter` – arober11 Aug 25 '23 at 13:24
93

As you are using Python 3, the module has been renamed to tkinter, as stated in the documentation:

Note Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
76

If you're using python 3.9 on Mac, you can simply install tkinter using brew:

brew install python-tk@3.9

This fixed it for me.

Edit:
As mentioned by others, you can also use the general command to install the latest version:

brew install python-tk
P1NHE4D
  • 1,106
  • 9
  • 14
54

For windows 10, it is important to check in the Python install the optional feature "tcl/tk and IDLE". Otherwise you get a ModuleNotFoundError: No module named 'tkinter'. In my case, it was not possible to install tkinter after the Python install with something like "pip install tkinter"

Andi Schroff
  • 1,156
  • 1
  • 12
  • 18
  • 4
    You are right, Python "embeddable zip file" don't contain tk, we should download the "executable installer" and don't forget to select the option "tcl/tk and IDLE" – tinyhare Feb 07 '20 at 14:14
  • 24
    You can amend a python installation launching again the python installer and selecting "Modify". At that point you can check the "tcl/tk and IDLE" checkbox and get the module you need. – Sergio Morstabilini Apr 09 '20 at 09:06
  • As another note, Python's "embeddable zip file" doesn't contain pip, so that has to be installed first. (From beginner python user) – rveach Jul 23 '22 at 16:30
37

To install the Tkinter on popular Linux distros:

Debian/Ubuntu:

sudo apt install python3-tk -y  

Fedora:

sudo dnf install -y python3-tkinter

Arch:

sudo pacman -Syu tk --noconfirm 

REHL/CentOS6/CentOS7:

sudo yum install -y python3-tkinter

OpenSUSE:

sudo zypper in -y python-tk
bfontaine
  • 18,169
  • 13
  • 73
  • 107
amzy-0
  • 435
  • 4
  • 5
  • -1; [`tkintertable`](https://pypi.org/project/tkintertable/) is not the same thing as the built-in [`tkinter`](https://docs.python.org/3/library/tkinter.html) module. – Mark Amery Nov 27 '21 at 13:29
  • 3
    Still: `import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'` – Dr.jacky Dec 29 '21 at 15:29
  • tkinter is a standard-library module, and can't be installed with pip. `tkintertable` **is not the same thing**, that's just a project that builds on top of tkinter. – Martijn Pieters Apr 25 '22 at 18:45
  • 1
    Note that this seems not to fix existing virtual environments, but only ones created after this point. – tsbertalan May 24 '22 at 16:51
  • 1
    @tsbertalan virtual environments can be created in varying ways depending on configuration options and the platform. If the entire standard-library folder was symlinked rather than being copied, the virtual environment should be updated automatically. Otherwise it will need to be recreated. – Karl Knechtel Apr 25 '23 at 19:35
24

For Mac use:

brew install python-tk
imxitiz
  • 3,920
  • 3
  • 9
  • 33
Diego Medeiros
  • 463
  • 5
  • 9
19

You might need to install for your specific version, I have known cases where this was needed when I was using many versions of python and one version in a virtualenv using for example python 3.7 was not importing tkinter I would have to install it for that version specifically.

For example

sudo apt-get install python3.7-tk 

No idea why - but this has occured.

deMangler
  • 427
  • 3
  • 14
11

Installing Tkinter

python -m pip install tk-tools

or

sudo apt install python3-tk

  • 2
    None of the pip commands worked for me (in a python 3.8 virtualenv). All that worked was the apt one. – tsbertalan Nov 15 '21 at 18:51
  • 4
    I'm confused by why multiple answers here propose using `pip` to install arbitrary PyPI modules that *depend* on the built-in `tkinter` module (like `tk-tools` here, or `tkintertable` in an answer below) as a solution to the built-in `tkinter` module not being available. It seems unlikely that that could possibly help, and even if it does somehow work, it's a pretty ugly solution, since you're left with the arbitrary third-party module installed that you probably don't want. – Mark Amery Nov 27 '21 at 13:34
  • 1
    **Do not use Pip. It cannot help solve the problem.** – Karl Knechtel Apr 25 '23 at 19:37
10

For Windows 10 using either VSCode or PyCharm with Python 3.7.4 - make sure Tk is ticked in the install. I tried import tkinter as xyz with upper/lower t and k's and all variants without luck.

What works is:

import tkinter
import _tkinter
tkinter._test()

An example in action:

import tkinter
import _tkinter

HEIGHT = 700
WIDTH = 800

root = tkinter.Tk()

canvas = tkinter.Canvas(root, height = HEIGHT, width=WIDTH)
canvas.pack()

frame = tkinter.Frame(root, bg='red')
frame.pack()

root.mainloop()
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 3
    I don't think direclty importing `_tkinter` has any effect. `tkinter` automatically will import that. – Bryan Oakley Jan 17 '20 at 14:17
  • @BryanOakley *I don't think this has any effect*. I can swear on anything this was the fix and it was for at least 5 others. It's got to be a bug then. – Jeremy Thompson Jan 18 '20 at 09:35
  • 3
    Why is importing _tkinter needed? What is the problem this addresses? How is this problem solved? Programming should be deterministic and not randomly trying things. On Ubuntu 20.04, I could remove this line and the script worked. – Frederick Ollinger Dec 07 '20 at 05:43
  • 2
    That probably deserves a new question,. This is Windows 10 specific and while python is x-platform it's not seamless. – Jeremy Thompson Dec 07 '20 at 11:56
  • See https://stackoverflow.com/q/25905540/4539999 - python 3 `import tkinter` # notice lowercase 't' in tkinter here – flywire Aug 25 '23 at 22:17
7

check the python version you have installed by using command python --version

check for the Tk module installed correctly from following code

sudo apt-get install python3-tk 

Check if you are using open-source OS then

check the tkinter module in the following path /home/python/site-packages/tkinter change the path accordingly your system

barbsan
  • 3,418
  • 11
  • 21
  • 28
6

On CentOS7, to get this working with Python2, I had to do:

yum -y install tkinter

Noting this here because I thought that there would be a pip package, but instead, one needs to actually install an rpm.

5

Make sure that when you are running your python code that it is in the python3 context. I had the same issue and all I had to do was input the command as:

sudo python3 REPLACE.py

versus

sudo python REPLACE.py

the latter code is incorrect because tkinter is apparently unnavailable in python1 or python2.

Owen Preece
  • 61
  • 1
  • 5
  • Why are you running with sudo? Python does not require it. – Frederick Ollinger Dec 07 '20 at 05:40
  • 3
    Python doesn't but if you are using linux and have the python file in a restricted section it can help. – Owen Preece Dec 12 '20 at 04:25
  • 1
    ... it might help. But you should operate on principle of least access. Randomly applying `sudo` to things is a recipe for bad irreversible mistakes. Don't `sudo` unless it's actually needed. – ocodo May 25 '22 at 01:47
3

You just need to install it and import them your project like that :

this code import to command line :

sudo apt-get install python3-tk 

after import tkinter your project :

from tkinter import *
  • 2
    Generally, it's discouraged to import * because there could be possible package conflicts. It also makes it difficult for other developers to easily see what was imported and how: https://stackoverflow.com/questions/2386714/why-is-import-bad – Frederick Ollinger Dec 07 '20 at 05:46
3

I resolved my issue in the PyCharm do following

  1. Install Python Interpreter from https://www.python.org/
  2. PyCharm > Preferences > Python Interpreter > Add
  3. Select installed interpreter
  4. In the run configuration select the newly installed interpreter

I also made a video instruction what I did https://youtu.be/awaURBnfwxk

Dima Portenko
  • 3,443
  • 5
  • 35
  • 48
3
$ sudo apt-get install python3.10-tk
Udesh
  • 2,415
  • 2
  • 22
  • 32
3

For Windows I had to reinstall python and make sure that while installing in Optional Features I had "tcl/tk and IDLE" enabled.

Sout parl
  • 89
  • 9
2

Tkinter should come with the latest Python, I don't think it comes with Python2. I had the same problem but once. I upgraded to Python 3.8 Tkinter was installed.

Eloni
  • 29
  • 4
1

tkinter comes with python... uninstall python, reinstall it, you're done

PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
1

if it doesnot work in pycharm you can add the module in the project interpreter by searching in +button python-tkinter and download it.

santosh ghimire
  • 169
  • 1
  • 10
0

Check apt for tasks, it may be marked for removed

sudo apt autoremove

Then check and install needed

0

We can use 2 types of methods for importing libraries

  1. work with import library
  2. work with from library import *

You can load tkinter using these ways:

  1. from tkinter import*

  2. import tkinter

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
0

On Linux it is possible to have installed two different versions of Python in my case 3.11 and 3.10. Only 3.10 was working with tkinter. 3.10 binary was located in my /usr/bin/python3 and 3.11 was located in /usr/local/sbin/python3. You can either specifically source the version you need or if you are SURE you don't need 3.11 at the moment, you can sudo cp /usr/bin/python3 /usr/local/sbin/python3 assuming your working version is in bin like mine is.

Poseidon
  • 165
  • 1
  • 8
-1
try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter 
except ImportError:
    try:
        # for Python3
        from tkinter import *   ## notice lowercase 't' in tkinter here
    except:
        try:
            print "Download Tkinter" ##python 2
        except SyntaxError:
            print("Download Tkinter") ##python 3
-1

If you have pip on your path, you could (in your command prompt) just type pip install tkinter Most versions of python already come with tkinter.

GL32
  • 36
  • 1
  • 1
-1

You'll probably need to install Tkinter. You can do so like this in the Windows command prompt:

pip install tk
AngusAU293
  • 19
  • 7
-2

--------- WORKED ON PYTHON 2.7------------

Install all below packages

sudo apt-get install git
sudo apt-get install python-tk
sudo apt-get install python-pip
sudo apt install picolisp
sudo -H pip2 install --upgrade pip
sudo pip install -I pillow
sudo apt-get install python-imaging-tk
sudo apt-get install python-tk
RAHUL
  • 23
  • 4
  • 4
    Why are you installing `git`, and `PIL`? Also why are you installing another programming language (`picolisp`)? Btw you are installing tkinter (`sudo apt-get install python-tk`) twice. You can shorten this answer to just: `sudo pip install --upgrade pip` and `sudo apt-get install python-tk`. – TheLizzard Jul 06 '21 at 09:54
-2

Firstly you should test your python idle to see if you have tkinter:

import tkinter

tkinter._test()

Trying typing it, copy paste doesn't work.

So after 20 hours of trying every way that recommended on those websites figured out that you can't use "tkinter.py" or any other file name that contains "tkinter..etc.py". If you have the same problem, just change the file name.

fcdt
  • 2,371
  • 5
  • 14
  • 26
-4

cmd - terminal

pip install tkinter