[PYTHON] pyenv doesn't work with tcl-tk installed by homebrew.

Problems encountered

When I tried to use tkinter from python installed using pyenv on Mac, I got the following error.

Traceback (most recent call last):
  File "./annotate.py", line 3, in <module>
    import tkinter
  File "/Users/???/.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'

The environment is

How Gugu was found (and didn't solve)

Reinstall python with pyenv

If you have python installed with pyenv, uninstalling it and installing it again seems to solve the problem.

I found the following page:

Specifically, it feels like ↓.

$ pyenv versions
 system

However, when I run tkinter again ...

$ python -m tkinter
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed 
in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
  File "/Users/???/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/???/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File 
"/Users/???/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__main__.py", line 7, in <module>
main()
  File "/Users/???/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 3988, in _test
    root = Tk()
  File "/Users/???/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 2025, in __init__
    self._loadtk()
  File "/Users/???/.pyenv/versions/3.7.4/lib/python3.7/tkinter/__init__.py", line 2040, in _loadtk
    % (_tkinter.TK_VERSION, tk_version))
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

I get another error. In this case, the header is compiled using version 8.6, but it seems that the library referenced at runtime is responsible for version 8.5.

Even though I googled, I couldn't find a Japanese solution.

solution

When I googled in English, I found the next page.

This is it.

In other words, edit / usr / local / Cellar / pyenv / VERSION / plugins / python-build / bin / python-build and apply the following patch (although the public bath is "!" 1 Rewrite the line).

diff -c python-build.orig python-build
*** python-build.orig	2019-12-10 17:47:04.000000000 +0900
--- python-build.new	2019-12-11 11:53:17.000000000 +0900
***************
*** 772,778 ****
        export CC=clang
      fi
      ${!PACKAGE_CONFIGURE:-./configure} --prefix="${!PACKAGE_PREFIX_PATH:-$PREFIX_PATH}" \
 !       $CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
    ) >&4 2>&1
  
    { "$MAKE" $MAKE_OPTS ${!PACKAGE_MAKE_OPTS} "${!PACKAGE_MAKE_OPTS_ARRAY}"
--- 772,778 ----
        export CC=clang
      fi
      ${!PACKAGE_CONFIGURE:-./configure} --prefix="${!PACKAGE_PREFIX_PATH:-$PREFIX_PATH}" \
 !       $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            ' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
    ) >&4 2>&1
  
    { "$MAKE" $MAKE_OPTS ${!PACKAGE_MAKE_OPTS} "${!PACKAGE_MAKE_OPTS_ARRAY}"

So you can reinstall python with pyenv.

$ pyenv uninstall 3.7.5
$ pyenv install 3.7.5
$ python -m tkinter

It went well.

Recommended Posts

pyenv doesn't work with tcl-tk installed by homebrew.
PIL cannot be installed by virtualenv with pyenv
Install the latest Python from pyenv installed with homebrew
When matplotlib doesn't work with python2.7
Intellisense doesn't work with tensorflow2.0 + VScode
I couldn't use tkinter with python installed by pyenv of anyenv
Pip doesn't work with PyDev in Pleiades
Use Anaconda-Python installed with pyenv from openFrameworks
I wanted to install Python 3.4.3 with Homebrew + pyenv
Make OpenCV3 available from python3 installed with pyenv
Homebrew Vim doesn't start with Python 3.8 error Note
Solved the problem that MacVim installed by Homebrew was not built by python of pyenv
Tensorflow doesn't work!
When Pipenv's pyenv support (automatic version installation) doesn't work
Create a virtual environment with Anaconda installed via Pyenv
Use Python installed with pyenv for PL / Python execution environment
Use python installed with Pyenv with Sublime REPL of Sublime Text 3