Resolve: ImportError: No module named ‘Tkinter’
This error generally occurs because of a missing Tkinter module in your Python. Let’s see how you can resolve it in simple steps.
Spelling Mistake
Generally, with Python3, the spelling of the Tkinter module is tkinter
, i.e., all lowercase. So, if you use Python3, try importing by modifying the spelling. The proper code is given below:
import tkinter as tk
Installing Tkinter module
If, after modifying the spelling, the problem still persists, then it might be due to the Tkinter module not being installed on your laptop. Mostly, Python3 comes with a pre-installed Tkinter module, but if it shows an import error, you should install it.
You can install it using the below code:
sudo apt-get install python3-tk
Note:- This sudo
command is used to get administrative permissions from your Terminal. It is specifically used on Linux/Unix-based operating systems. To grant the administrative permissions on Windows, run your command prompt as run as administrator
.
Or if you have brew
on your laptop, then you can simply install it using brew command:
brew install python3-tk
After installing the module, import it using the correct case spelling. You will see no ImportError
now, and the error has been resolved as the import is successful.
For Windows:
Generally, the Tkinter module comes pre-installed with Python3 now. If you are facing the issue, just re-install Python, and during installation, click on Customise Installation
and after that, check in the option td/tk and IDLE
under Optional Features.
Leave a Reply