This week I wanted to play around with
tkinter a bit. But this proved to be difficult on my M2 Mac.
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()"
And there was much rejoicing!
brew install email@example.com↩︎