ModuleNotFoundError: No module named 'tkinter' in Python


Borislav Hadzhiev

Mon Apr 18 20222 min read

ModuleNotFoundError: No module named 'tkinter' in Python #

The Python "ModuleNotFoundError: No module named 'tkinter'" occurs when tkinter is not installed in our Python environment. To solve the error, install the module and import is as import tkinter as tk.

no module named tkinter

Open your terminal and run the following command to install tkinter.

# 👇️ === UBUNTU / DEBIAN === sudo apt-get install python3-tk # 🚨 Make sure to specify correct Python version. # For example, my Python 3.10, so I would install as sudo apt-get install python3.10-tk # 👇️ === MacOS === brew install python-tk@3.10 # 🚨 Make sure to specify correct Python version. # For example, if you run Python v3.9 run adjust command to brew install python-tk@3.9 # 👇️ === Fedora === sudo dnf install python3-tkinter # 👇️ === CentOS === sudo yum install python3-tkinter
If you are on Windows, you have to make sure to check the optiontcl/tk and IDLE when installing Python.

If you already installed Python, download the installer, run it and click Modify. Then check the tcl/tk and IDLE checkbox to install tkinter for your Python version.

Now you should be able to import and use the tkinter module.
import tkinter as tk from tkinter import ttk root = tk.Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="Hello World!").grid(column=0, row=0) ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) root.mainloop()
Note that in Python 3, tkinter is imported using a lowercase t, whereas in Python 2, it is imported with an uppercase T

If your code can be ran using both Python 3 and 2, use a try/except statement for a universal import.
try: import tkinter as tk # using Python 3 from tkinter import ttk except ImportError: import Tkinter as tk # falls back to import from Python 2

We try to import the tkinter module (Python 3) and if we get an ImportError, we know the file is being ran in Python 2, so we import using an uppercase T and alias the import to tk.

If you aren't sure what version of Python you're using, run the python --version command.

python --version

get python version

If you are in a virtual environment, the version of Python corresponds to the version that was used to create the virtual environment.

If the error persists, make sure you haven't named a module in your project as because that would shadow the original tkinter module.

You also shouldn't be declaring a variable named tkinter as that would also shadow the original module.

Use the search field on my Home Page to filter through my more than 1,000 articles.