pyenv Install tkinter on macOS

参考链接
错误信息

import turtle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/.pyenv/versions/3.7.4/lib/python3.7/turtle.py", line 107, in <module>
    import tkinter as TK
  File "~/.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'

install pyenv via homebrew
$brew install pyenv

install tcl-tk via home brew
$brew install tcl-tk

to get tcl-tk 8.6 to work with the pyenv install of python, you must
edit the python-build script file. It may be located in one these two locations:

~/.pyenv/plugins/python-build/bin/python-build

or:

/usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build

Once you have the script file open, search for:
$CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
and replace with:
$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' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

install Python 3.7.4 in pyenv
$pyenv install 3.7.4

set the default python version via the global variable and check it
$pyenv global 3.7.4
$pyenv version

Add to your .bash_profile to set the pyenv every time you load your shell
$echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

reset your shell
$exec "$SHELL"

check your environment

$which python
$python -V
$which pip
$pip -V

(check if your pip needs upgrading with $pip install --upgrade pip)

Test your tcl-tk installation with $python -m tkinter -c ‘tkinter._test()’

If you don’t get any errors when running the above test, your installation should work

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值