Home > Article > Backend Development > Why am I getting a \"ModuleNotFoundError: No module named \'Tkinter\'\" when importing Tkinter in Python?
ModuleNotFoundError: No module named 'Tkinter'
When attempting to import the Tkinter module in Python (or tkinter in Python 3), some users may encounter this error:
ModuleNotFoundError: No module named 'Tkinter' or ModuleNotFoundError: No module named 'tkinter'
Cause and Solution:
This error occurs when the Tkinter module is not installed on your system. To resolve this issue, you need to install the appropriate package for your operating system and Python version. Here are some examples:
sudo apt-get install python3-tk
sudo dnf install python3-tkinter
You can also specify the Python version number, such as:
sudo apt-get install python3.7-tk sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64
After installing the package, restart your Python interpreter and try importing Tkinter (or tkinter in Python 3) again. If you are using both Python 2 and 3, you can ensure compatibility by importing the Tkinter module based on the Python version:
import sys if sys.version_info[0] == 3: import tkinter as tk else: import Tkinter as tk
The above is the detailed content of Why am I getting a \"ModuleNotFoundError: No module named \'Tkinter\'\" when importing Tkinter in Python?. For more information, please follow other related articles on the PHP Chinese website!