Help us understand the problem. What is going on with this article?

pyenvのpythonでtkinterを使用する方法

Issue

pyenvでinstallしたpythonでpython -m tkinterimport tkinterをしたときに以下のエラーが発生。
ネット上の記事を見ると、brew install tcl-tkで治るなど書いてあったが、上手くいかなかった。
しかし、どうやら最近リリースされたpyenvのv1.2.14でtcl-tkとの互換性が修正されたらしく、GitHubのissueで解決策も書いてあったのでシェアします。

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

Solution

  • Upgrade pyenv (>=1.2.14)
brew upgrade pyenv
  • Install tcl-tk
brew install tcl-tk
  • Set environment variables

    • If you use bash
    bash_profile
    export PATH="/usr/local/opt/tcl-tk/bin:$PATH"
    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"
    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'"
    
    • If you use fish
    config.fish
    set -x PATH "/usr/local/opt/tcl-tk/bin" $PATH
    set -x LDFLAGS "-L/usr/local/opt/tcl-tk/lib"
    set -x CPPFLAGS "-I/usr/local/opt/tcl-tk/include"
    set -x PKG_CONFIG_PATH "/usr/local/opt/tcl-tk/lib/pkgconfig"
    set -x 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'"
    
  • Reinstall python via pyenv

pyenv install 3.7.4
  • Run tkinter
python -m tkinter
  • Solved :beer:

Reference

skyloken
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした