简体   繁体   English

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

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

If I do this on MacOS Mojave in Homebrew:如果我在 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()'    

I get:我得到:

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'

I've been searching for days, and have tried various solutions such as here but without luck so far.我一直在寻找几天,并尝试了各种解决方案,例如这里,但到目前为止没有运气。 Any ideas why I'm getting this error and how to fix it?任何想法为什么我会收到此错误以及如何解决它?

Thanks in advance!提前致谢!

I never got this to work.我从来没有让这个工作。 I spent a ton of time on 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.目前 Homebrew Python 和 Tcl-Tk 似乎不是同一版本,这是一个常见问题。 What I did end up doing was to install both pyenv and anaconda side by side as per the excellent instructions of @Simba here , and using Anaconda my Tcl-Tk commands are working fine.我最终所做的是按照@Simba here的出色说明并排安装 pyenv 和 anaconda ,并使用 Anaconda 我的 Tcl-Tk 命令运行良好。 Posting here so that others don't fall down this rabbit hole, or if they do and find a solution, it would be wonderful to know.在这里发帖,这样其他人就不会掉进这个兔子洞,或者如果他们这样做并找到解决方案,很高兴知道。

Here is step by step guide to make IDLE and tkinter work:这是使 IDLE 和tkinter工作的分步指南

  1. install tcl-tk with Homebrew.使用 Homebrew 安装tcl-tk In shell run brew install tcl-tk在 shell 运行brew install tcl-tk
  2. in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc在 shell 中运行echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
  3. reload shell by quitting Terminal app or run source ~/.zshrc通过退出Terminal应用程序或运行source ~/.zshrc .zshrc 重新加载 shell
  4. after reloaded check that tck-tk is in $PATH .重新加载后检查tck-tk是否在$PATH中。 Run echo $PATH | grep --color=auto tcl-tk运行echo $PATH | grep --color=auto tcl-tk echo $PATH | grep --color=auto tcl-tk . echo $PATH | grep --color=auto tcl-tk As the result you should see your $PATH contents with tcl-tk highlighted结果,您应该看到您的 $PATH 内容并突出显示tcl-tk
  5. now we run three commands from Homebrew's output from step #1现在我们从步骤#1 运行 Homebrew 的 output 中的三个命令
    1. in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"在 shell 中运行export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
    2. in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"在 shell 中运行export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
    3. in shell run export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"在 shell 中运行export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
  6. if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall <your python version> .如果您已经使用 pyenv 安装了pyenv版本,则使用pyenv uninstall <your python version>其卸载。 Eg pyenv uninstall 3.8.2例如pyenv uninstall 3.8.2
  7. set environment variable that will be used by python-build .设置将由python-build使用的环境变量。 In shell run 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'" Note: in future use tck-tk version that actually installed with Homebrew.在 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'"注意:以后使用 Homebrew 实际安装的tck-tk版本。 At the moment of posting 8.6 was the actual在发布8.6的那一刻是实际的
  8. finally install Python with pyenv with pyenv install <version> .最后用 pyenv 安装pyenvpyenv install <version> Eg pyenv install 3.8.2例如pyenv install 3.8.2

Test测试

  1. in shell run pyenv global <verion that you've just installed>在 shell 中运行pyenv global <verion that you've just installed>
  2. now check IDLE.现在检查空闲。 In shell run idle .在 shell 中idle You should see IDLE window without any warnings and "text printed in red".您应该看到 IDLE window 没有任何警告和“以红色打印的文本”。

IDLE 窗口从终端运行。没有警告

  1. now check tkinter .现在检查tkinter In shell run python -m tkinter -c "tkinter._test()" .在 shell 中运行python -m tkinter -c "tkinter._test()" You should see test window like on the image:您应该在图像上看到测试 window :

tkinter 测试窗口

That's it!而已!

My environment:我的环境:

check this is something went wrong executing steps above:检查执行上述步骤是否出现问题:

  1. macOS Catalina macOS 卡塔利娜
  2. zsh (included in macOS Catalina) = "shell" above zsh (包含在 macOS Catalina 中)=上面的“shell”
  3. Homebrew (installed with instructions from Homebrew official website) Homebrew(使用 Homebrew 官方网站的说明安装)
  4. pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub) pyenv (安装 Homebrew 和 PATH 根据 GitHub 的pyenv官方自述文件更新)
  5. Python 3.8.x - 3.9.x (installed with pyenv install <version> command) Python 3.8.x - 3.9.x (使用pyenv install <version>命令安装)

Of course it doesn't work for Python from pyenv .当然,它不适用于 pyenv 的pyenv

pyenv is a Python environment manager that supports installation of different version of Python. pyenv是一个 Python 环境管理器,支持安装不同版本的 Python。 When you install a Python from pyenv , pyenv will download the source code and build it on your local machine.当您从pyenv安装 Python 时, pyenv将下载源代码并在您的本地计算机上构建它。

The problem is that Python built by pyenv doesn't use tcl-tk installed by Homebrew as a dependency.问题是 pyenv 构建的pyenv不使用 Homebrew 安装的tcl-tk作为依赖项。

The issue you mentioned have explained this clear enough.你提到的问题已经足够清楚地解释了这一点。 You have to reinstall/re-compile a Python by pyenv with special PYTHON_CONFIGURE_OPTS environment variable, to tell pyenv use the tcl-tk installed by Homebrew as a dependency.您必须使用特殊的PYTHON_CONFIGURE_OPTS环境变量通过pyenv重新安装/重新编译 Python,以告诉pyenv使用 Homebrew 安装的tcl-tk作为依赖项。

I had the same issue when I tried to install tkinter through pyenv.当我尝试通过 pyenv 安装tkinter时遇到了同样的问题。 Although @Dribbler answer was helpful to find out that anaconda is more compatible, when I followed @Simba instructions, I still got an error that says that there is no conda command .尽管@Dribbler 的回答有助于发现 anaconda 更兼容,但当我按照@Simba 的说明进行操作时,我仍然收到一个错误,提示there is no conda command Finally, I was able to fix it using the following in case someone has the same problem and still want to stick with pyenv .最后,我可以使用以下方法修复它,以防有人遇到同样的问题并且仍然想坚持使用pyenv

The Fix修复

  1. I followed @nickolay instructions to install tkinter and set the path the proper way.我按照@nickolay 的说明安装tkinter并以正确的方式设置了路径。

  2. Then, I installed anaconda3-2020.07 using the pyenv install anaconda3-2020.07 command.然后,我使用pyenv install anaconda3-2020.07命令安装了 anaconda3-2020.07。

  3. Because I am using pyenv python 3.8.6 globally : I navigated to the folder I want to use tkinter and used the anaconda3-2020.07 locally by utilizing the command pyenv local anaconda3-2020.07 to use this version of pyenv in that specific folder.因为我在全局范围内使用 pyenv python 3.8.6:我导航到我想使用 tkinter 的文件夹,并通过在该特定文件夹中使用命令pyenv local anaconda3-2020.07 anaconda3-2020.07使用此版本的 pyenv。 It ran without errors!它运行没有错误!

在此处输入图像描述

Note: I am using the following script in the .bash_profile to trigger the virtualenv automatically when cd the desired directory注意:我在.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 -)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM