Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Why does `tkinter` (or `turtle`, or IDLE) seem to be missing or broken? Isn't it part of the standard library?

+4
−0

I had understood that Python is supposed to come "batteries included", and that the "batteries" specifically include:

  • the tkinter standard library, a simple GUI framework

  • a simple IDE called IDLE, implemented using tkinter

  • the turtle standard library, which uses tkinter to implement turtle graphics (useful for teaching Python)

However, none of these is working on my system:

>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> import turtle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/turtle.py", line 107, in <module>
    import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'

and the IDLE program is nowhere to be found.

I've similarly heard that Python compiled from source might give an error like:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/path/to/python/tkinter/__init__.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

What gives? I know that these components are definitely part of the standard library.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+3
−0

General

This question and answer are adapted from my original write-up on Stack Overflow, which you may consult for more details. This version is crafted as to be a clear overview, without referring to a huge pile of previous Q&A about specific special circumstances. You may also be interested in the guide provided by the official Tk documentation.

The short explanation: Although Tkinter is indeed part of the standard library, several distributions deliberately exclude it. It may also be broken due to missing system support. See the appropriate section below to find out how to add Tkinter support for your system.

Do not use pip to try to solve the problem. The Pip package manager cannot help here. No part of the Python standard library - including tkinter, turtle etc. - can be installed from PyPI[1]. For security reasons, PyPI now blocks packages using names that match the standard library.

There are many packages on PyPI that may look suitable, but are not. Most are simply wrappers that try to add a little functionality to the standard library Tkinter. However, one especially problematic package is turtle. It should not be there, because current policy (since 2017) is to block packages with names that match the standard library; but it was uploaded long before that, and has not been maintained since. It is Python 2.x specific code that will break during installation on Python 3, is extremely out of date (published in 2009) and - most importantly - has nothing whatsoever to do with turtle graphics. I have an outstanding support ticket to try to get it delisted from PyPI - and over a year later, it appears that something may soon be done about this.

If IDLE seems to be missing but tkinter works properly

Try python -m idlelib to run IDLE directly from Python. There might just not be a wrapper provided for this (or it might not be on the PATH for some reason).

Linux

Using the system-provided Python

tkinter is often deliberately excluded (perhaps just to save space). The downstream components (IDLE and turtle) may also be excluded, or they might just fail at runtime.

Use the system package manager to install Python tkinter library support. Examples:

# Debian/Ubuntu (and Mint, Pop! OS etc.)
apt-get install python3-tk
# (If that doesn't work, try specifying a minor version, e.g.:)
apt-get install python3.12-tk
# (Legacy systems where the system Python is a 2.x version)
apt-get install python-tk

# Fedora
dnf install python3-tkinter
# (or use yum instead on RHEL)

# Arch
pacman -S tk

When building from source

First, of course, make sure that you haven't explicitly excluded Tkinter support in the configuration options.

Most commonly, however, the problem occurs because of missing system support. Please install the corresponding development package for Tcl/Tk with your system package manager before attempting to build.

This is also necessary when using Pyenv to set up a new environment, since it generally works by downloading and building from source.

# Debian/Ubuntu (and Mint, Pop! OS etc.)
apt-get install tk-dev

This is separate from any package installed for the system Python: e.g. apt-get install python3-tk will not enable Tkinter on Debian for a separate copy of Python built from source but only for the system Python. The development package will allow Python to build an additional _tkinter module which is a "binding" to the main package.

Windows

When Python is installed on Windows using the official installer, there is an option to include or exclude Tcl/Tk support. Simply "repair" the installation and make sure to check the option to include this.

MacOS using Brew

Use brew install python-tk; if necessary, specify a Python version like brew install python-tk@3.11.

For non-system installations, it may be necessary to re-install and specify that Tkinter support should be included, like brew install python --with-tcl-tk.

Headless environments

It's generally not possible to install Tkinter - or any other GUI toolkit - for headless server environments like PythonAnywhere or Amazon Linux EC2. The code will run on a remote server, so there is no monitor to display the GUI; while it would be possible in principle for the code to send commands back to the client that the client could then use to create a GUI, in general the server will have no knowledge of the client's environment. Making this work would require setting up some communication protocol ahead of time (such as X11).

Virtual environments

Virtual environments will generally have the same Tkinter support as the Python installation they're based upon. Therefore, start by fixing the "base" installation that the virtual environment is based upon. However, this won't always work. If it doesn't, you will probably have to re-create the virtual environment from scratch, after adding Tkinter support to the base.

It is not possible to add or remove Tkinter support for an individual virtual environment. This is because a virtual environment only differs from its base in terms of the site-packages, and there is no such package for Tkinter (since, again, it is a standard library component that cannot be obtained using Pip).


  1. In some rare cases, PyPI hosts backports of standard library modules for earlier versions of Python. These should not be considered part of the standard library of the versions they support - and download statistics suggest that a large fraction of these downloads are mistaken, i.e., applied to versions where they are unnecessary and can only cause problems. In at least one case, to my understanding, there was also a draft implementation of a future standard library module published on PyPI, before it was accepted. This, too, should not be downloaded or installed now. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »