Explain why you might encounter the error – no module named tkinter – when you are writing a python program and how you can get around this error.
Hey there! This article will explain why you might encounter the error – no module named Tkinter – when you are writing a python program and how you can get around this error.
The Tkinter is a module that provides functionality to create graphical user interfaces. It will enable you to create windows, checkboxes, etc. It is a built-in module; therefore, it is included when you installed python.
So if it’s installed, why is there an error? The reason is the Tkinter module was renamed in Python version 3. In python 2, the module was initially called Tkinter. After that, in python 3, the name of the module changed to
Firstly, let’s see how to check if the tkinter is installed with python. You can do so by running the following command from your terminal:
python -m tkinter
In case you have several python installations on your laptop, make sure you are using the correct one when running the command. For instance, in my case, I have two installations, python 2 and python3.
And when I run the command to check if tkinter is installed I see the following:
python -m tkinter
/usr/local/opt/python@2/bin/python2.7: No module named tkinter.__main__; 'tkinter' is a package and cannot be directly executed
This means that when I run the
python command on my machine, I am using version 2. So if
tkinter is a built-in module, why is there an error indicating it can’t find the
tkinter module? The reason is the
tkinter module is actually called
Tkinter(uppercase) in python2.
In other words, module names in Python are case sensitive, and in the case of the tkinter module, the module name was initially named
Tkinter in version 2, and changed to
tkinter in Python version 3.
You can check the python version running on your machine by running the below command from your terminal:
Consequently if I run the same command but using python3, the tkinter sample window will open. See the command below:
python3 -m tkinter
If this window pops up, it means the tkinter module is included in your python installation.
The easiest way to fix this problem is by upgrading your python to use python 3. If upgrading python is not an option, you only need to rename your imports to use
Tkinter(uppercase) instead of
Check if Tkinter is available in python 2 running the following command from your terminal:
python -m Tkinter
As a result, this window proves that your python installation includes the Tkinter module.
In conclusion, if the error no module named tkinter raises, it probably means you are using python 2. Firstly, you can get around the problem by upgrading to python 3.
Additionally, you can rename your import to
Tkinter, which is how the module was named in python 2, just if upgrading is not an option.
I hope this article was useful, and thanks for reading and supporting this blog. Happy coding! 🙂
Become an ethical hacker that can hack computer systems like black hat hackers and secure them like security experts.
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java