问题
I recently spent couple hours making tkinter and IDLE work on my pyenv Python installation (macOS).
Why you are here?
- You manage Python versions with
pyenvon macOS and - ( You want IDLE - the development environment for Python - work on your macOS
- or you want
tkintermodule work )
What's wrong?
You get one of the following errors:
Python may not be configured for Tkonimport tkinterimport _tkinter # If this fails your Python may not be configured for TkRuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)ModuleNotFoundError: No module named '_tkinter'
回答1:
Here is step by step guide to make IDLE and tkinter work:
- install
tcl-tkwith Homebrew. In shell runbrew install tcl-tk - in shell run
echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc - reload shell by quitting
Terminalapp or runsource ~/.zshrc - after reloaded check that
tck-tkis in$PATH. Runecho $PATH | grep --color=auto tcl-tk. As the result you should see your $PATH contents withtcl-tkhighlighted - now we run three commands from Homebrew's output from step #1
- in shell run
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib" - in shell run
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include" - in shell run
export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
- in shell run
- if you have your Python version already installed with
pyenvthen uninstall it withpyenv uninstall <your python version>. E.g.pyenv uninstall 3.8.2 - set environment variable that will be used by
python-build. In shell runPYTHON_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 usetck-tkversion that actually installed with Homebrew. At the moment of posting8.6was the actual - finally install Python with
pyenvwithpyenv install <version>. E.g.pyenv install 3.8.2
Test
- in shell run
pyenv global <verion that you've just installed> - now check IDLE. In shell run
idle. You should see IDLE window without any warnings and "text printed in red".
- now check
tkinter. In shell runpython -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:
- macOS Catalina
zsh(included in macOS Catalina) = "shell" above- Homebrew (installed with instructions from Homebrew official website)
pyenv(installed with Homebrew and PATH updated according topyenvofficial readme from GitHub)- Python
3.8.x-3.9.x(installed withpyenv install <version>command)
来源:https://stackoverflow.com/questions/61870688/cant-run-idle-with-pyenv-installation-python-may-not-be-configured-for-tk-m