Can't run IDLE with pyenv installation: `Python may not be configured for Tk` `ModuleNotFoundError: No module named _tkinter'

前端 未结 1 445
执念已碎
执念已碎 2021-01-16 03:45

I recently spent couple hours making tkinter and IDLE work on my pyenv Python installation (macOS).

Why you are here?

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 04:22

    Here is step by step guide to make IDLE and tkinter work:

    1. install tcl-tk with Homebrew. In shell run brew install tcl-tk
    2. in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
    3. reload shell by quitting Terminal app or run source ~/.zshrc
    4. after reloaded check that 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 highlighted
    5. now we run three commands from Homebrew's output from step #1
      1. in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
      2. in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
      3. in shell run export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
    6. if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall . E.g. pyenv uninstall 3.8.2
    7. set environment variable that will be used by 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
    8. finally install Python with pyenv with pyenv install . E.g. pyenv install 3.8.2

    Test

    1. in shell run pyenv global
    2. now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".

    1. now check 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:

    1. macOS Catalina
    2. zsh (included in macOS Catalina) = "shell" above
    3. Homebrew (installed with instructions from Homebrew official website)
    4. pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub)
    5. Python 3.8.x - 3.9.x (installed with pyenv install command)

    0 讨论(0)
提交回复
热议问题