3

If I do this on MacOS Mojave in Homebrew:

$ brew install tcl-tk
$ brew install pyenv
$ pyenv install 3.7.4
$ pyenv global 3.7.4
$ python -m tkinter -c 'tkinter._test()'    

I get:

Traceback (most recent call last):
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/Users/craign/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/Users/craign/.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'

I've been searching for days, and have tried various solutions such as here but without luck so far. Any ideas why I'm getting this error and how to fix it?

Thanks in advance!

Dribbler
  • 4,343
  • 10
  • 33
  • 53

3 Answers3

6

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 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)
nickolay
  • 3,643
  • 3
  • 32
  • 40
4

I never got this to work. I spent a ton of time on this page as well as a number of other sites, and tried everything. It seems that Homebrew Python and Tcl-Tk are at this time not out of the box the same version, and it's a common issue. What I did end up doing was to install both pyenv and anaconda side by side as per the excellent instructions of @Simba here, and using Anaconda my Tcl-Tk commands are working fine. Posting here so that others don't fall down this rabbit hole, or if they do and find a solution, it would be wonderful to know.

Dribbler
  • 4,343
  • 10
  • 33
  • 53
  • I actually managed to get this working on a laptop, but then following the exact same instructions didn't work on iMac I have. – fny Jan 24 '20 at 17:34
1

I had the same issue when I tried to install tkinter through pyenv. Although @Dribbler answer was helpful to find out that anaconda is more compatible, when I followed @Simba instructions, I still got an error that says that there is no conda command. Finally, I was able to fix it using the following in case someone has the same problem and still want to stick with pyenv.

The Fix

  1. I followed @nickolay instructions to install tkinter and set the path the proper way.

  2. Then, I installed anaconda3-2020.07 using the pyenv install anaconda3-2020.07 command.

  3. Because I am using pyenv python 3.8.6 globally: I navigated to the folder I want to use tkinter and used the anaconda3-2020.07 locally by utilizing the command pyenv local anaconda3-2020.07to use this version of pyenv in that specific folder. It ran without errors!

enter image description here

Note: I am using the following script in the .bash_profile to trigger the virtualenv automatically when cd the desired directory

# manage python version using pyenv
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

# add pyenv virtualenv
eval "$(pyenv virtualenv-init -)"
Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21