pyenv 파이썬에서 tkinter를 사용하는 방법

5447 단어 Tkinter파이썬pyenv

Issue



pyenv에서 install한 python에서 python -m tkinter 또는 import tkinter를 할 때 다음 오류가 발생합니다.
넷상의 기사를 보면, brew install tcl-tk 로 낫는 등 써 있었지만, 잘 안 갔다.
그러나 분명히 최근에 릴리스 된 pyenv의 v1.2.14에서 tcl-tk와의 호환성이 수정 된 것 같습니다.
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

  • Reference


  • GitHub의 이슈
  • 좋은 웹페이지 즐겨찾기