Easy Fix for Module Not Found Error ‘TKinter’

5/5 - (1 vote)

“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 python3-tkinter

πŸ’‘ If you don't have PIP or it doesn't work
python -m pip install python3-tkinter
python3 -m pip install python3-tkinter

πŸ’‘ If you have Linux and you need to fix permissions (any one):
sudo pip3 install python3-tkinter
pip3 install python3-tkinter --user

πŸ’‘ If you have Linux or Ubuntu with apt
sudo apt-get install python3-tk

πŸ’‘ If you're using macOS and you want to install Python 3.9 Tkinter (any):
brew install python-tk@3.9
brew install python-tk

πŸ’‘ If you have Fedora
sudo dnf install python3-tkinter

πŸ’‘ If you have Windows and you have set up the py alias
py -m pip install python3-tkinter

πŸ’‘ If you have Anaconda
conda install -c anaconda python3-tkinter

πŸ’‘ If you have Jupyter Notebook
!pip install python3-tkinter
!pip3 install python3-tkinter

πŸ’¬ Summary: You see that most solutions concern a combination of the installation tool (e.g., pip or brew or conda) and any of the library name variations python3-tkinter, python-tkinter, python3-tk, or python-tk.

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.

The following code snippet was proposed to overcome these issues for different Python versions (2 vs 3):

Instead of:

import tkinter

You can run:

import sys
if sys.version_info[0] == 3:
    import tkinter as tk
    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 where python

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 pip file.

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

  • The ModuleNotFoundError may 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

import tkinter

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 import statement. 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 ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

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)

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 ImportError.

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 ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

How to Fix : β€œImportError: Cannot import name X” in Python?

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

How to Call a Function from Another File in Python?