I began a course taught by Christian Koch called “Learn Python by Building 10 Apps with tkinter”. I soon realized I couldn’t use tkinter
even though it’s a built-in Python module. Wait, what?
Well, if you use pyenv
to manage your Python versions (which you should), you’ll run into this issue. I spent a lot of time trying to figure out the right solution, so I wanted to share it with you.
Step 1: Installing Necessary System Packages
Using tkinter
with pyenv
can be tricky due to missing dependencies.
First, let’s make sure we have installed the necessary system packages for tkinter
.
In most Linux systems, you can install them with the following commands:
On macOS, you can use Homebrew to achieve the same:
Step 2: Linking the Correct Tcl/Tk Versions
Next, make sure you link the correct versions of Tcl/Tk when installing Python. pyenv
builds Python in your environment, but if you don’t have the required dependencies, like the Tk/Tcl libraries, it’ll build Python without them.
If the Python version you want to use is already installed on your system, you’ll need to uninstall it before proceeding. For example:
When installing a new Python version with pyenv
, use the following commands:
For Linux:
For macOS, after installing tcl-tk with brew:
Replace <version>
with the version of Python you want to install. After that, you should be able to import tkinter
in your pyenv
Python environment.
These steps should help you fix the tkinter
and pyenv
issue.
Note
I couldn’t get tkinter
to work with pyenv
on Python 3.11.0
, but it worked perfectly on 3.11.2
.