pyenvでインストールしたpythonでtkinterがimportできなかった時。 

# 概要
PySimpleGuiを使おうとしたところ`_tkinter`というモジュールがありませんと言われてから解決するまでの流れ

## 環境
- macOS 10.14.6（18G87）
- pyenv(anyenv)
- python 3.8.5
- venv

## ModuleNotFoundError: No module named '_tkinter'

`_tkinter`とは？
- UnixのWindowでアプリケーションを作るための何か
- （後日ちゃんと調べて更新）

この質問を参考にしてpythonを再インストール
[[stack overflow] Unable to install tkinter with pyenv Pythons on MacOS](https://stackoverflow.com/questions/60469202/unable-to-install-tkinter-with-pyenv-pythons-on-macos)

```bash:issue
$ python -m tkinter
Traceback (most recent call last):
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/runpy.py", line 185, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/runpy.py", line 144, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/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'
```

### `tcl-tk`をインストール
`tcl-tk`とは？
- （後日ちゃんと調べて更新）

```bash:tcl-tk(install)
$ brew install tcl-tk
```

### 環境変数設定
`$ brew info`で表示された情報をとりあえずなにも考えずに実行
- （後日ちゃんと調べて更新）

```bash:tcl-tk(info)
$ brew info tcl-tk
tcl-tk: stable 8.6.10 (bottled) [keg-only]
Tool Command Language
https://www.tcl-lang.org
/usr/local/Cellar/tcl-tk/8.6.10 (3,036 files, 51MB)
  Poured from bottle on 2020-09-02 at 11:04:37
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/tcl-tk.rb
License: TCL
==> Dependencies
Required: openssl@1.1 ✔
==> Caveats
tcl-tk is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have tcl-tk first in your PATH run:
  echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.bash_profile

For compilers to find tcl-tk you may need to set:
  export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
  export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"

For pkg-config to find tcl-tk you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"

==> Analytics
install: 8,121 (30 days), 23,761 (90 days), 93,541 (365 days)
install-on-request: 4,323 (30 days), 12,391 (90 days), 51,230 (365 days)
build-error: 0 (30 days)
```


```bash:環境変数設定
$ echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.bash_profile
$ export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
$ export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
$ export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
```

### pythonを再インストール

```bash:python3.8.5のアンインストール
$ pyenv uninstall 3.8.5
pyenv: remove ~/.anyenv/envs/pyenv/versions/3.8.5? [y|N]y
pyenv: 3.8.5 uninstalled
```

```bash:python3.8.5のインストール
$ pyenv install 3.8.5
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.5.tar.xz...
-> https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
Installing Python-3.8.5...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.5 to ~/.anyenv/envs/pyenv/versions/3.8.5
```

## RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
今度はバージョンが合っていない？ようなエラーが出てきたので以下の記事を参考にpython-buildの設定を修正
[[Qiita] pyenv が homebrew でインストールした tcl-tk と動かない。](https://qiita.com/survivor7777777/items/5a8e23d30822437ae9f9)

```bash:issue
$ python sample.py 
~/workspace/python/pysimplegui/myvenv/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py:17865: UserWarning: You are running a VERY old version of tkinter 8.5.9
  warnings.warn('You are running a VERY old version of tkinter {}'.format(tclversion_detailed), UserWarning)
Traceback (most recent call last):
  File "sample.py", line 13, in <module>
    event, values = window.read()
  File "~/workspace/python/pysimplegui/myvenv/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 7568, in Read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
  File "~/workspace/python/pysimplegui/myvenv/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 7623, in _read
    self._Show()
  File "~/workspace/python/pysimplegui/myvenv/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 7395, in _Show
    StartupTK(self)
  File "~/workspace/python/pysimplegui/myvenv/lib/python3.8/site-packages/PySimpleGUI/PySimpleGUI.py", line 12716, in StartupTK
    Window.hidden_master_root = tk.Tk()
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/tkinter/__init__.py", line 2263, in __init__
    self._loadtk()
  File "~/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/tkinter/__init__.py", line 2279, in _loadtk
    raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
```
`python-build`を編集してpythonを再インストール

```bash
$ cd .anyenv/envs/pyenv/plugins/python-build/bin
$ cp -p python-build python-build.org
$ vim python-build
# ↑記事参照↑
$ pyenv uninstall 3.8.5
$ pyenv install 3.8.5
```

これで一旦`tkinter`を利用したコードが動くようになりました。
途中、思考停止で通り過ぎていたものがあるので後日更新します。