The following error occurs when
python -m tkinter or ʻimport tkinter
is executed with python installed by pyenv. When I looked at the article on the net, it said that it could be cured withbrew install tcl-tk
, but it didn't work. However, it seems that the recently released pyenv v1.2.14` fixes compatibility with tcl-tk, on GitHub issues. The solution was also written, so I will share it.
Traceback (most recent call last): File "/Users/ken/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 183, in _run_module_as_ma in mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "/Users/ken/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 142, in _get_module_detai ls return _get_module_details(pkg_main_name, error) File "/Users/ken/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 109, in _get_module_detai ls __import__(pkg_name) File "/Users/ken/.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'
brew upgrade pyenv
brew install tcl-tk
Set environment variables
If you use bash
export PATH="/usr/local/opt/tcl-tk/bin:$PATH" export LDFLAGS="-L/usr/local/opt/tcl-tk/lib" export CPPFLAGS="-I/usr/local/opt/tcl-tk/include" export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig" export 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'"
- If you use fish ```shell:config.fish set -x PATH "/usr/local/opt/tcl-tk/bin" $PATH set -x LDFLAGS "-L/usr/local/opt/tcl-tk/lib" set -x CPPFLAGS "-I/usr/local/opt/tcl-tk/include" set -x PKG_CONFIG_PATH "/usr/local/opt/tcl-tk/lib/pkgconfig" set -x 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'"
pyenv install 3.7.4
python -m tkinter