DEV Community

Cover image for Using tkinter with pyenv: A Simple Two-Step Guide
Max Shapira
Max Shapira

Posted on

28 3

Using tkinter with pyenv: A Simple Two-Step Guide

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?

Traceback (most recent call last):
  File "/Users/maxshapira/Development/public/tkinter-basics/main.py", line 1, in <module>
    import tkinter as tk
  File "/Users/maxshapira/.pyenv/versions/3.11.0/lib/python3.11/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'

Enter fullscreen mode Exit fullscreen mode

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:

sudo apt-get update
sudo apt-get install tk-dev
Enter fullscreen mode Exit fullscreen mode

On macOS, you can use Homebrew to achieve the same:

brew install tcl-tk
Enter fullscreen mode Exit fullscreen mode

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:

pyenv uninstall 3.11.3
Enter fullscreen mode Exit fullscreen mode

When installing a new Python version with pyenv, use the following commands:

For Linux:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install <version>

Enter fullscreen mode Exit fullscreen mode

For macOS, after installing tcl-tk with brew:

brew install openssl readline sqlite3 xz zlib

env LDFLAGS="-L$(brew --prefix openssl@1.1)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix sqlite3)/lib -L$(brew --prefix xz)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix tcl-tk)/lib" \
CPPFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix readline)/include -I$(brew --prefix sqlite3)/include -I$(brew --prefix xz)/include -I$(brew --prefix zlib)/include -I$(brew --prefix tcl-tk)/include" \
PKG_CONFIG_PATH="$(brew --prefix openssl@1.1)/lib/pkgconfig:$(brew --prefix readline)/lib/pkgconfig:$(brew --prefix sqlite3)/lib/pkgconfig:$(brew --prefix xz)/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig:$(brew --prefix tcl-tk)/lib/pkgconfig" \
pyenv install <version>

Enter fullscreen mode Exit fullscreen mode

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.

Note that I couldn't get tkinter to work with pyenv on Python 3.11.0, but it worked perfectly on 3.11.2.

These steps should help you fix the tkinter and pyenv issue.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (6)

Collapse
 
javascriptdude profile image
Timothy C. Quinn

Thanks. I've been using pyenv for years and have always had issues with tkinter. I appreciate digging into this.

Note, I had to change a couple things for my Ubuntu 22.04 install:

  1. Instead of package python-openssl, I used python3-openssl
  2. Needed to specify the --with-tcltk-includes and --with-tcltk-includes switches: env PYTHON_CONFIGURE_OPTS="--enable-shared --with-tcltk-includes=/usr/include/tcl --with-tcltk-libs=\"/usr/lib/tcl8.6 /usr/lib/tk8.6\"" pyenv install <version>

Note: /include/tcl contains both tcl and tk headers on Ubuntu 22.04 LTS

Collapse
 
nathanchilton profile image
Nathan Chilton

Thank you! I just ran into this problem when installing the latest Python version on a freshly set-up installation of Ubuntu 24.04, and your instructions resolve my issue.

Collapse
 
javascriptdude profile image
Timothy C. Quinn

I just installed on a pristine Linux Mint 21.3 and your script ran perfectly as-is. Thanks again!

Collapse
 
xshapira profile image
Max Shapira

No problem. Happy to help 👍

Collapse
 
lewiscowles1986 profile image
Lewis Cowles

Okay so using mac with homebrew, all I needed was:

brew install python-tk

followed by

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.11.8

this is because 3.11.8 was not installed prior, so if you had it installed, just uninstall it first.

I didn't need to mess around with any of the build env vars specified.

Collapse
 
esteban_garviso_b9555576c profile image
Esteban Garviso

This happend when you use FOP instean of OOP.

medium.com/bitmountn/oop-vs-pop-vs...

nextjs tutorial video

Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay