I recently spent couple hours making tkinter
and IDLE work on my pyenv
Python installation (macOS).
Why you are here?
pyenv
on macOS and
tkinter
module work )
What's wrong?
You get one of the following errors:
Python may not be configured for Tk
on import tkinter
import _tkinter # If this fails your Python may not be configured for Tk
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
ModuleNotFoundError: No module named '_tkinter'
Here is step by step guide to make IDLE and tkinter
work:
tcl-tk
with Homebrew. In shell run brew install tcl-tk
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
Terminal
app or run source ~/.zshrc
tck-tk
is in $PATH
. Run echo $PATH | grep --color=auto tcl-tk
. As the result you should see your $PATH contents with tcl-tk
highlightedexport 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"
pyenv
then uninstall it with pyenv uninstall <your python version>
. E.g. pyenv uninstall 3.8.2
python-build
. In shell run 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'"
Note: in future use tck-tk
version that actually installed with Homebrew. At the moment of posting 8.6
was the actual
pyenv
with pyenv install <version>
. E.g. pyenv install 3.8.2
Test
pyenv global <verion that you've just installed>
idle
. You should see IDLE window without any warnings and "text printed in red".tkinter
. In shell run 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
pyenv
(installed with Homebrew and PATH updated according to pyenv
official readme from GitHub)3.8.x
- 3.9.x
(installed with pyenv install <version>
command)Not sure why the above didn't work for me. What did work after hours of headache was:
$ brew install python-tk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With