参考链接
错误信息
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