How To Solve ModuleNotFoundError: No module named ‘tkinter’ in Python

ModuleNotFoundError: No module named 'tkinter' in Python

Tkinter is a built-in user interface module in Python. When the error ModuleNotFoundError: No module named ‘tkinter’ in Python appears, it may be because the module is faulty or not installed. This article will show you how to reinstall it on different operating systems.

What causes the error ModuleNotFoundError: No module named ‘tkinter’ in Python

The cause of this error may be that the Tkinter module has not been installed in the Python environment. Let’s look at the causes of this error on different operating systems.

The cause of the error occurs on macOS.

The cause of the error on the macOS operating system can happen because we install Tkinter with the command “pip

Example:

pip install tk

Sometimes the “pip” command won’t work as expected on macOS.

The cause of the error occurs on Windows.

Tkinter is already available in Python. This error may be because it has been deleted or corrupted. The error will be displayed as follows.

ModuleNotFoundError: No module named 'tkinter'

The cause of the error occurs on Linux.

The problem ModuleNotFoundError: No module called ‘tkinter’ on Linux can potentially be caused by this module not being installed into the Python environment, or by typing the incorrect module name since in Python 3 it has been changed to lowercase “tkinter,” instead of “Tkinter” in Python 2.

In Python version 2:

import Tkinter as tk

In Python version 3:

import tkinter as tk

Solutions for ModuleNotFoundError: No module named ‘tkinter’ in Python

The solution is straightforward: install Tkinter or reinstall Python. Tkinter is already available in Python and needs to be aware of the name when importing in different Python versions. Here is the workaround for each different operating system.

Reinstall on macOS operating system

On macOS, we should use “brew” to install Tkinter. “brew” can also use to install all kinds of software packages on macOS.

brew install python-tk

Reinstall on Windows operating system

Reinstall Python by downloading the required version. Remember to check “Add Python to PATH” and “tcl/tk and IDLE” when installing. Then just import and use.

Example:

from tkinter import *

window = Tk()
window.title("learnshareit")
window.mainloop()

Reinstall on Linux operating system

On Linux operating system, we can reinstall using the following syntax

Ubuntu or Debian operating systems

sudo apt-get install python3-tk

CentOS operating systems

sudo yum install python3-tkinter

Fedora operating systems

sudo dnf install python3-tkinter

To check if Tkinter is installed on Linux, we use this command.

python3 -m tkinter

If you see the message ModuleNotFoundError: No module named ‘tkinter’ in Python means that Tkinter is not installed yet.

You can review a similar article about this error for a better understanding.

Summary

Through the article, we have understood why the error occurs and how to solve it on different operating systems. The fix is straightforward. We need to reinstall Python, and Python already has Tkinter. Hope this article was helpful to you. Thanks for reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *