“1995 is calling… it wants its user interfaces back!”
Sorry, I couldn’t let this go. 😂 TKinter is actually a great Python library and you can use it to make modern user interfaces too. This tutorial shows you how to fix the
No Module Named 'tkinter' error.
Quick Fix: 👇
Python raises the
ImportError: No module named 'tkinter' when it cannot find the library
tkinter. Tkinter is part of Python’s standard library, so for modern Python installations, you can just
import tkinter without needing to install it first. But in some environments, you may need to install it using
pip install python3-tkinter or variants thereof.
Alternatively, you may have different Python versions on your computer, and
tkinter is not installed for the particular version you’re using.
Here are some variants you may want to try to fix the error and install
tkinter in your environment:
💡 If you have only one version of Python installed:
pip install python3-tkinter💡 If you have Python 3 (and, possibly, other versions) installed:
pip3 install💡 If you don't have PIP or it doesn't work
python -m pip install💡 If you have Linux and you need to fix permissions (any one):
python3 -m pip install
sudo pip3 install💡 If you have Linux or Ubuntu with apt
sudo apt-get install💡 If you're using macOS and you want to install Python 3.9 Tkinter (any):
brew install email@example.com brew install python-tk💡 If you have Fedora
sudo dnf install python3-tkinter💡 If you have Windows and you have set up the
py -m pip install💡 If you have Anaconda
conda install -c anaconda💡 If you have Jupyter Notebook
💬 Summary: You see that most solutions concern a combination of the installation tool (e.g.,
conda) and any of the library name variations
But as I mentioned before, likely, you don’t need to install it because it’s already part of Python’s standard library, so a simple
import statement should be sufficient if your Python is correctly installed!
🌍 Recommended Tutorial: How to Install Python?
Speaking of the
import statement—older versions of Tkinter came with some confusion about the capitalization of its name.
In Python 3, the module was renamed from
Tkinter (Python 2) to
tkinter (Python 3), as stated in the docs:
👉 Note: Tkinter has been renamed to tkinter in Python 3. The
2to3 tool will automatically adapt imports when converting your sources to Python 3.
You can run:
import sys if sys.version_info == 3: import tkinter as tk else: import Tkinter as tk
To import Tkinter independently of your concrete (local) Python version.
By the way, this is how the error may look when you try to import the tkinter library into your (virtual) environment and Python raises the following
ImportError: No module named tkinter:
>>> import tkinter Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import tkinter ModuleNotFoundError: No module named 'tkinter'
Windows Solution Idea: Fix the Path
The error might persist even after you have installed the
tkinter library. This likely happens because
pip is installed but doesn’t reside in the path you can use. Although
pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using
pip in the correct path.
To fix the problem with the path in Windows follow the steps given next.
Step 1: Open the folder where you installed Python by opening the command prompt and typing
Step 2: Once you have opened the
Python folder, browse and open the
Scripts folder and copy its location. Also verify that the folder contains the
Step 3: Now open the
Scripts directory in the command prompt using the
cd command and the location that you copied previously.
Step 4: Now install the library using
pip install tkinter command. Here’s an analogous example:
After having followed the above steps, execute our script once again. And you should get the desired output.
Other Solution Ideas
ModuleNotFoundErrormay appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
- You may have mixed up Python and pip versions on your machine. Use Python 3 and you should be able to import Tkinter without error.
- You can also check out this article to learn more about possible problems that may lead to an error when importing a library.
Understanding the “import” Statement
In Python, the
import statement serves two main purposes:
- Search the module by its name, load it, and initialize it.
- Define a name in the local namespace within the scope of the
importstatement. This local name is then used to reference the accessed module throughout the code.
What’s the Difference Between ImportError and ModuleNotFoundError?
What’s the difference between
You can see this in this screenshot from the docs:
You can also check this relationship using the
issubclass() built-in function:
>>> issubclass(ModuleNotFoundError, ImportError) True
Specifically, Python raises the
ModuleNotFoundError if the module (e.g.,
tkinter) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an
If an import statement cannot import a module, it raises an
ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a
The following video shows you how to resolve the
The following video shows you how to import a function from another folder—doing it the wrong way often results in the
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.