Can't run IDLE with pyenv installation: `Python may not be configured for Tk` `ModuleNotFoundError: No module named _tkinter' [duplicate]
Asked Answered
C

2

11

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

Why you are here?

  1. You manage Python versions with pyenv on macOS and
  2. ( You want IDLE - the development environment for Python - work on your macOS
  3. or you want tkinter module work )

What's wrong?

You get one of the following errors:

  1. Python may not be configured for Tk on import tkinter
  2. import _tkinter # If this fails your Python may not be configured for Tk
  3. RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
  4. ModuleNotFoundError: No module named '_tkinter'
Compensate answered 18/5, 2020 at 13:28 Comment(0)
C
17

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 <your python version>. 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 <version>. E.g. pyenv install 3.8.2

Test

  1. in shell run pyenv global <verion that you've just installed>
  2. now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".

IDLE window run from Terminal. No warnings

  1. now check tkinter. In shell run python -m tkinter -c "tkinter._test()". You should see test window like on the image:

tkinter test window

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 <version> command)
Compensate answered 18/5, 2020 at 13:28 Comment(5)
In step 7 above, setting the environment variable PYTHON_CONFIGURE_OPTS should start with export keyword, no? Otherwise, this worked great for me.Increment
Both of you have saves my life. I tried other approaches and they didn't work. @PapaJoeDee was right, I needed to add export to step 7 in order for it to work.Walls
My environment are: Big Sur 11.3.1, Xcode 12.5, Python 3.8.2 Apple clang version 12.0.5 (clang-1205.0.22.9) Target: x86_64-apple-darwin20.4.0 ---- This saved my day man! Thanks so much nickolay. Also, don't skip any step, any step here is necessary to get the tk work.Induce
This worked! My tk-tl was flickering and not displaying anything when I tried using Turtle beforehand. Sure enough, despite being on python 3.10.2 through pyenv, it was still using tk-tl 8.5.9. Followed these steps and it was fixed. Note: for Apple M1 chips, the path is /opt/homebrew, not /opt/localMidship
Thank you. I need this and this worked for me with pyenv 2.2.5 on macOS 12.3.1 and on arm (Apple M1). Note that home-brew installs in /opt/homebrew on Macs running Apple Silicon, so setting the PATH was different in my case.Nantucket
S
2

Not sure why the above didn't work for me. What did work after hours of headache was:

$ brew install python-tk
Standin answered 10/5, 2021 at 6:29 Comment(1)
Homebrew does the job if you're going to use only one python version on your OS. For multiple python versions e.g. managed with pyenv you'll have to go beyond HomebrewCompensate

© 2022 - 2024 — McMap. All rights reserved.