tcl-tk (tkinter) 不通过 Homebrew pyenv 在 MacOS Mojave 上安装

tcl-tk (tkinter) not installing via Homebrew pyenv on MacOS Mojave

如果我在 Homebrew 的 MacOS Mojave 上执行此操作:

$ brew install tcl-tk
$ brew install pyenv
$ pyenv install 3.7.4
$ pyenv global 3.7.4
$ python -m tkinter -c 'tkinter._test()'    

我得到:

Traceback (most recent call last):
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

我已经搜索了好几天,并尝试了各种解决方案,例如 here,但到目前为止都没有成功。我为什么会收到此错误以及如何修复它有什么想法吗?

提前致谢!

我从来没有让这个工作过。我在 this page as well as a number of other sites, and tried everything. It seems that Homebrew Python and Tcl-Tk are at this time not out of the box the same version, and it's a common issue. What I did end up doing was to install both pyenv and anaconda side by side as per the excellent instructions of @Simba 上花费了大量时间,使用 Anaconda 我的 Tcl-Tk 命令运行良好。在这里发帖是为了让其他人不会掉进这个兔子洞,或者如果他们掉进了这个兔子洞并找到了解决方案,那将是一件很棒的事情。

这里是 分步指南 让 IDLE 和 tkinter 工作:

  1. 使用 Homebrew 安装 tcl-tk。在shell运行brew install tcl-tk
  2. 在shell运行echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
  3. 通过退出 Terminal 应用或 运行 source ~/.zshrc
  4. 重新加载 shell
  5. 重新加载后检查 tck-tk$PATH 中。 运行echo $PATH | grep --color=auto tcl-tk。结果你应该看到你的 $PATH 内容突出显示 tcl-tk
  6. 现在我们 运行 来自步骤 #1 的 Homebrew 输出的三个命令
    1. 在shell运行export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
    2. 在shell运行export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
    3. 在shell运行export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
  7. 如果您的 Python 版本已经通过 pyenv 安装,则使用 pyenv uninstall <your python version> 将其卸载。例如。 pyenv uninstall 3.8.2
  8. 设置 python-build 将使用的环境变量。在 shell 运行 PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'" 注意:将来使用 tck-tk 实际安装 Homebrew 的版本。在发布时 8.6 是实际的
  9. 最终安装 Python 和 pyenv 以及 pyenv install <version>。例如。 pyenv install 3.8.2

测试

  1. 在shell运行pyenv global <verion that you've just installed>
  2. 现在检查 IDLE。在shell运行idle。您应该看到没有任何警告的 IDLE window 和 "text printed in red".

  1. 现在检查 tkinter。在shell运行python -m tkinter -c "tkinter._test()"。您应该会看到测试 window,如图所示:

就是这样!

我的环境:

检查执行上述步骤是否出了问题:

  1. macOS 卡特琳娜
  2. zsh(包含在 macOS Catalina 中)= "shell" 以上
  3. Homebrew(按照Homebrew官网说明安装)
  4. pyenv(根据 GitHub 的 pyenv 官方自述文件更新了 Homebrew 和 PATH)
  5. Python 3.8.x - 3.9.x(使用 pyenv install <version> 命令安装)

我在尝试通过 pyenv 安装 tkinter 时遇到了同样的问题。虽然@Dribbler 的回答有助于发现 anaconda 更兼容,但当我按照@Simba 的说明进行操作时,我仍然收到一个错误,提示 there is no conda command。 最后,我能够使用以下方法修复它,以防有人遇到同样的问题但仍想坚持使用 pyenv.

修复

  1. 我按照@nickolay 的说明安装了 tkinter 并以正确的方式设置了路径。

  2. 然后,我使用pyenv install anaconda3-2020.07命令安装了anaconda3-2020.07。

  3. 因为我正在使用 pyenv python 3.8.6 globally: 我导航到我想使用 tkinter 的文件夹并使用 anaconda3-2020.07 本地 通过使用命令 pyenv local anaconda3-2020.07 在该特定文件夹中使用此版本的 pyenv。它运行没有错误!

注意: 我在 .bash_profile 中使用以下脚本来在 cd 所需目录时自动触发 virtualenv

# manage python version using pyenv
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

# add pyenv virtualenv
eval "$(pyenv virtualenv-init -)"