Skip to main content
  1. Dispatches/

TIL - Getting asdf Python with tkinter working on a M2 Mac

·312 words·2 mins
TIL Macos Python Homebrew Tkinter
Daniel Andrlik
Author
Daniel Andrlik lives in the suburbs of Philadelphia. By day he manages product teams. The rest of the time he is a podcast host and producer, writer of speculative fiction, a rabid reader, and a programmer.

This week I wanted to play around with tkinter a bit. But this proved to be difficult on my M2 Mac.

I use asdf to manage various runtime versions, so first I checked to see if it was already working with my existing Python installation using the built in test method. This did not work out.

python -m tkinter -c "tkinter._test()"
Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 112, in _get_module_details
  File "/Users/daniel/.asdf/installs/python/3.11.2/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'

OK, so clearly that module had not been created when asdf had compiled that runtime. That’s easy enough to correct. I installed tcl-tk formula and installed a new Python runtime to test it out.

brew install tcl-tk
# Brew output here...
asdf install python 3.11.4

Then I switched over to that runtime and tried the test command again. No love.

I was aware that Homebrew can install a version of Python with the needed libraries1, but I didn’t want to be beholden to Homebrew’s Python installation while developing. So I went to web searching. I found a number of answers that worked for people using older OS versions and M1 chips, but none of those worked for my M2 Mac running Ventura.

After narrowing my search to M2 Macs, I found the answer in this Github issue. For it to work I had to install pkg-config and then reinstall the Python runtime.

brew install pkg-config
# Brew output ...
asdf uninstall python 3.11.4
asdf reshim python
PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='$(pkg-config tk --cflags)' --with-tcltk-libs='$(pkg-config tk --libs)'" asdf install python 3.11.4

I ran the test command again…

python -m tkinter -c "tkinter._text()"

Test GUI window rendered by tkinter
The tkinter test window

And there was much rejoicing!


  1. brew install python-tk@3.11 ↩︎

Related

TIL about Sloth
·75 words·1 min
TIL Macos Lsof Utilities Homebrew Open Source
While looking at my Github news feed today, I spotted an app via one of Jeff Triplett’s stars. The app is Sloth by Sveinbjorn Thordarson, and it provides a GUI frontend for lsof that can quickly show you which processes are interacting with various files, directories, IP sockets, or Unix sockets.
TIL - Automatically Set Your virtualenv
·321 words·2 mins
TIL Python Poetry Hatch Venv Direnv
I’m a big fan of using direnv to set project-specific env variables. And at one point I was using it to automatically activate my poetry virtualenv whenever I entered the directory per this function.
TIL: Show Hidden Files in MacOS Finder
·94 words·1 min
TIL Macos Hotkeys
This is one of those silly little things that I always feel like I’m the last to learn. Admittedly, when I’m working with hidden files, I’m usually on the command line or in a text editor that already shows them.