RasmusGP
RasmusGP

Reputation: 5352

ImportError: No module named 'Tkinter'

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?

Upvotes: 470

Views: 1167158

Answers (27)

user8234870
user8234870

Reputation:

Use:

sudo apt-get install python3.10-tk

Upvotes: 9

P1NHE4D
P1NHE4D

Reputation: 1371

If you're using Python 3.9 on Mac, you can simply install tkinter using Homebrew:

brew install python-tk@3.9

This fixed it for me.

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

brew install python-tk

Upvotes: 99

Egemen İm
Egemen İm

Reputation: 9

In a cmd terminal (Windows):

pip install tkinter

Upvotes: -4

Diego Medeiros
Diego Medeiros

Reputation: 575

For Mac, use:

brew install python-tk

Upvotes: 31

Mehmet Nergiz
Mehmet Nergiz

Reputation: 23

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

Upvotes: -2

santosh ghimire
santosh ghimire

Reputation: 179

If it does not work in PyCharm, you can add the module in the project interpreter by searching in +button python-tkinter and download it.

Upvotes: 1

Sergey Shamanayev
Sergey Shamanayev

Reputation: 328

Check apt for tasks. It may be marked for removal.

sudo apt autoremove

Then check and install as needed.

Upvotes: 0

Frederick Ollinger
Frederick Ollinger

Reputation: 777

On CentOS 7, to get this working with Python 2, 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 package file.

Upvotes: 6

Jafar Choupan
Jafar Choupan

Reputation: 67

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

This code import to the command line:

sudo apt-get install python3-tk

After, import tkinter in your project:

from tkinter import *

Upvotes: 3

Jeremy Thompson
Jeremy Thompson

Reputation: 65682

For Windows 10, using either Visual Studio Code 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.

This works:

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

Upvotes: 10

Devaliya Pratik
Devaliya Pratik

Reputation: 89

Check the Python version you have installed by using command python --version.

Check for the Tk module is installed correctly from the following code:

sudo apt-get install python3-tk

Check if you are using open-source OS then.

Check the tkinter module in the path /home/python/site-packages/tkinter, and change the path accordingly on your system.

Upvotes: 8

Sout parl
Sout parl

Reputation: 99

For Windows, try to reinstall Python and make sure that during the installation process in the "Optional Features" it has the "tcl/tk and IDLE" option enabled.

Upvotes: 4

Andi Schroff
Andi Schroff

Reputation: 1344

For Windows, make sure to check in the Python install the optional feature "tcl/tk and IDLE". Otherwise you get:

ModuleNotFoundError: No module named 'tkinter'

If Python is already installed without this optional feature, you need to amend the installation by launching again the Python installer and selecting Modify.

NB: you won't be able pip install tkinter, don't bother trying!

Upvotes: 68

Poseidon
Poseidon

Reputation: 165

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.

Upvotes: 0

AMZYEI
AMZYEI

Reputation: 594

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

Upvotes: 49

GL32
GL32

Reputation: 46

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.

Upvotes: -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

Upvotes: -2

d-coder
d-coder

Reputation: 13993

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

Upvotes: 686

Dima Portenko
Dima Portenko

Reputation: 3854

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

Upvotes: 3

Mr.Programmer nope
Mr.Programmer nope

Reputation: 201

Installing Tkinter

python -m pip install tk-tools

or

sudo apt install python3-tk

Upvotes: 13

رضا آلناصر
رضا آلناصر

Reputation: 35

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

Upvotes: 1

Eloni
Eloni

Reputation: 29

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.

Upvotes: 2

deMangler
deMangler

Reputation: 477

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.

Upvotes: 23

RAHUL
RAHUL

Reputation: 23

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

Upvotes: -2

PythonProgrammi
PythonProgrammi

Reputation: 23453

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

Upvotes: 0

Owen Preece
Owen Preece

Reputation: 61

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.

Upvotes: 5

Burhan Khalid
Burhan Khalid

Reputation: 174682

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.

Upvotes: 94

Related Questions