[英]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
工作的分步指南:
tcl-tk
with Homebrew.tcl-tk
。 In shell run brew install tcl-tk
brew install tcl-tk
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
Terminal
app or run source ~/.zshrc
Terminal
应用程序或运行source ~/.zshrc
.zshrc 重新加载 shelltck-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
highlightedtcl-tk
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
pyenv
then uninstall it with pyenv uninstall <your python version>
.pyenv
版本,则使用pyenv uninstall <your python version>
其卸载。 Eg pyenv uninstall 3.8.2
pyenv uninstall 3.8.2
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.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 actual8.6
的那一刻是实际的pyenv
with pyenv install <version>
.pyenv
和pyenv install <version>
。 Eg pyenv install 3.8.2
pyenv install 3.8.2
Test测试
pyenv global <verion that you've just installed>
pyenv global <verion that you've just installed>
idle
.idle
。 You should see IDLE window without any warnings and "text printed in red".tkinter
.tkinter
。 In shell run python -m tkinter -c "tkinter._test()"
.python -m tkinter -c "tkinter._test()"
。 You should see test window like on the image: That's it!而已!
My environment:我的环境:
check this is something went wrong executing steps above:检查执行上述步骤是否出现问题:
zsh
(included in macOS Catalina) = "shell" above zsh
(包含在 macOS Catalina 中)=上面的“shell”pyenv
(installed with Homebrew and PATH updated according to pyenv
official readme from GitHub) pyenv
(安装 Homebrew 和 PATH 根据 GitHub 的pyenv
官方自述文件更新)3.8.x
- 3.9.x
(installed with pyenv install <version>
command) 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修复
I followed @nickolay instructions to install tkinter
and set the path the proper way.我按照@nickolay 的说明安装
tkinter
并以正确的方式设置了路径。
Then, I installed anaconda3-2020.07 using the pyenv install anaconda3-2020.07
command.然后,我使用
pyenv install anaconda3-2020.07
命令安装了 anaconda3-2020.07。
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.