MacOS下通过pyenv安装带tkinter的Python 3.11.4失败
问题:pyenv安装Python 3.11.4时出现_tkinter模块缺失错误
问题现象
在MacOS Ventura 13.5.2 (22G91)系统中,通过pyenv安装Python 3.11.4时触发以下错误:
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
但使用pyenv安装Python 3.10.4则完全正常:
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
已尝试的解决方案
尝试过卸载后通过环境变量指定tcl-tk路径重新安装的方法,具体操作及结果如下:
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
该方法并未解决问题。目前确认pyenv已识别Homebrew安装的tcl-tk,且brew info tcl-tk输出中未显示其为keg-only(无需额外设置环境变量)。
环境信息
以下是系统中已安装相关软件的brew info输出(均为最新版本):
tcl-tk
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)
python-tk@3.11
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)
pyenv
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)
Homebrew版本
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)
内容的提问来源于stack exchange,提问作者absoup
相关产品推荐
相关产品推荐

