2

I am trying to use Tkinter in a Python 3.10 project. I have created a virtual environment with Python 3.10 and have installed Tkinter. When I run import tkinter I get the following error.

Traceback (most recent call last):
  File "/home/xxx/yyy/main.py", line 11, in <module>
    import tkinter
  File "/usr/local/lib/python3.10/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

However, if I do the same outside of a virtual environment with Python 3.9, or create a new virtual environment with Python 3.9, it runs fine without error.

Is Tkinter not supported yet in 3.10, or could I be doing something else wrong?

KOB
  • 4,084
  • 9
  • 44
  • 88
  • It looks like your version of Python 3.10 is not built with Tk included. How did you install it on your system? Did you build it yourself? If so, you were probably missing some dependencies at compile time. If you installed binaries packaged by somebody else, it either was intentionally missing the Tk stuff (e.g. because they're in a different package), or they were built incorrectly. – Blckknght Jun 10 '22 at 07:19
  • @Blckknght I am using Ubuntu and am unsure how to include tk in the installation – KOB Jun 10 '22 at 07:57
  • this might be a duplicate question. does this answer work for you? https://stackoverflow.com/a/25905642/5156102 – mark jay Aug 24 '22 at 20:26
  • I'm using Python 3.10 under Window 11 and downloaded python from [python.org](https://www.python.org/downloads/) and did not had any trouble. – Thingamabobs Oct 20 '22 at 21:47

2 Answers2

4

I had the same problem.

I solved this installing python3-tk.

Depending your OS, do the appropriated command in terminal or cdm. For example:

# UBUNTU / DEBIAN
sudo apt-get install python3-tk

# MacOS
brew install python-tk@3.10

# Fedora
sudo dnf install python3-tkinter

# CentOS
sudo yum install python3-tkinter

# Make sure to specify correct Python version:
sudo apt-get install python3.10-tk
brew install python-tk@3.9

If you are on Windows, you have to make sure to tick the checkbox tcl/tk and IDLE when installing Python.

I'm using Pop_os, a Ubuntu based linux, so sudo apt-get install python3-tk worked for me.

I found this solution in https://bobbyhadz.com/blog/python-no-module-named-tkinter

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '22 at 07:15
  • 1
    This worked for me `sudo apt-get install python3.10-tk`, as mentioned in the link provided – Chris Nov 23 '22 at 08:56
  • 1
    Any hints on what to do if this doesn't solve the problem? I've got python-tk@3.10 installed for python 3.10.8 but i'm still getting an error "No module named '_tkinter'" – Schneems Dec 02 '22 at 21:18
0

tkinter is part of python standard library but it depend on underlying components namely tcl/tk, on linux distributions. If you had installed python manually via the configure/make commands, the tkinter module build will fail unless you have the tcl/tk packages and its devel packages (tk-devel/tcl-devel usually). If that is the case just install these packages and rebuild and install python via the configure/make commands.