Search code examples
pythonmacoshomebrewpyenv

Unable to install python 3.11.4 with tkinter via pyenv on MacOS


I'm receiving the following error trying to install python 3.11.4 via pyenv on MacOS Ventura 13.5.2 (22G91):

foo@bar:~$ pyenv install 3.11.4
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.11.4.tar.xz...
-> https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tar.xz
Installing Python-3.11.4...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/me/.pyenv/versions/3.11.4/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'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Installed Python-3.11.4 to /Users/me/.pyenv/versions/3.11.4

at the same time, I'm able to install python 3.10.4 via pyenv just fine:

foo@bar:~$ pyenv install 3.10.4
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.4.tar.xz...
-> https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
Installing Python-3.10.4...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.4 to /Users/me/.pyenv/versions/3.10.4

While I was searching for an answer, I found this SO question: https://stackoverflow.com/a/61879759/14688566

I've tried some of the recommended approaches like this one (uninstall, install with some env vars):

foo@bar:~$ pyenv uninstall 3.11.4
pyenv: remove /Users/me/.pyenv/versions/3.11.4? [y|N] y
pyenv: 3.11.4 uninstalled

foo@bar:~$ env \
PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
CPPFLAGS="-I$(brew --prefix tcl-tk)/include" \
PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
CFLAGS="-I$(brew --prefix tcl-tk)/include" \
PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6.13 -ltk8.6.13'" \
pyenv install 3.11.4

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.11.4.tar.xz...
-> https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tar.xz
Installing Python-3.11.4...
python-build: use tcl-tk from $PYTHON_CONFIGURE_OPTS
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/me/.pyenv/versions/3.11.4/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'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Installed Python-3.11.4 to /Users/me/.pyenv/versions/3.11.4

But it's not working either.

As far as I understand, pyenv successfully picks up homebrew's tcl-tk, so that's not an issue. According to this comment it appears tcl-tk is no longer keg-only and, therefore, setting environment variables is no longer needed. In fact, I don't see any caveats like "tcl-tk is keg-only, which means it was not symlinked into /usr/local" when I'm running brew info tcl-tk (as far as I undestand, that's the issue faced by the majority of commenters there and the fix for this is running pyenv install along with those environment variables).


Here are the outputs of brew info for tcl-tk, python-tk (not sure I even need this one installed tbh) and pyenv packages installed on my machine (all of them are latest versions) along with the brew ---version output:

foo@bar:~$ brew info tcl-tk
==> tcl-tk: stable 8.6.13 (bottled)
Tool Command Language
https://www.tcl-lang.org
Conflicts with:
  page (because both install `page` binaries)
/usr/local/Cellar/tcl-tk/8.6.13_5 (3,064 files, 52.8MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-09-12 at 20:45:30
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/t/tcl-tk.rb
License: TCL
==> Dependencies
Required: openssl@3 ✔
==> Caveats
The sqlite3_analyzer binary is in the `sqlite-analyzer` formula.
==> Analytics
install: 57,088 (30 days), 173,897 (90 days), 333,561 (365 days)
install-on-request: 27,654 (30 days), 89,791 (90 days), 164,180 (365 days)
build-error: 6 (30 days)
foo@bar:~$ brew info python-tk
==> python-tk@3.11: stable 3.11.5 (bottled)
Python interface to Tcl/Tk
https://www.python.org/
/usr/local/Cellar/python-tk@3.11/3.11.5 (5 files, 131.7KB) *
  Poured from bottle using the formulae.brew.sh API on 2023-09-12 at 11:11:06
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/python-tk@3.11.rb
License: Python-2.0
==> Dependencies
Required: python@3.11 ✔, tcl-tk ✔
==> Analytics
install: 7,261 (30 days), 16,597 (90 days), 32,528 (365 days)
install-on-request: 7,255 (30 days), 16,583 (90 days), 32,505 (365 days)
build-error: 0 (30 days)
foo@bar:~$ brew info pyenv
==> pyenv: stable 2.3.26 (bottled), HEAD
Python version management
https://github.com/pyenv/pyenv
/usr/local/Cellar/pyenv/2.3.26 (1,110 files, 3.3MB) *
  Poured from bottle using the formulae.brew.sh API on 2023-09-13 at 09:42:44
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/pyenv.rb
License: MIT
==> Dependencies
Required: autoconf ✔, openssl@3 ✔, pkg-config ✔, readline ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 109,431 (30 days), 278,276 (90 days), 531,993 (365 days)
install-on-request: 108,805 (30 days), 276,683 (90 days), 528,966 (365 days)
build-error: 3 (30 days)
foo@bar:~$ brew --version
Homebrew 4.1.11
Homebrew/homebrew-core (git revision 8e188d78fe0; last commit 2023-09-13)
Homebrew/homebrew-cask (git revision 862ffb4b59; last commit 2023-09-13)

Solution

  • reported this issue to pyenv team: https://github.com/pyenv/pyenv/issues/2790

    should be fixed with this PR: https://github.com/pyenv/pyenv/pull/2791