How to use tkinter with python in pyenv

Issue

The following error occurs when python -m tkinter or ʻimport tkinteris executed with python installed by pyenv. When I looked at the article on the net, it said that it could be cured withbrew install tcl-tk, but it didn't work. However, it seems that the recently released pyenv v1.2.14` fixes compatibility with tcl-tk, on GitHub issues. The solution was also written, so I will share it.

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

brew upgrade pyenv
brew install tcl-tk

  - If you use fish

      ```shell: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'"
pyenv install 3.7.4
python -m tkinter

Reference

Recommended Posts

How to use tkinter with python in pyenv
How to use python put in pyenv on macOS with PyCall
Python: How to use async with
How to use SQLite in Python
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use FTP with Python
How to use PubChem in Python
[Introduction to Python] How to use class in Python?
How to work with BigQuery in Python
How to use __slots__ in Python class
How to use Pyenv
How to use regular expressions in Python
How to use is and == in Python
How to deal with python installation error in pyenv (BUILD FAILED)
How to use the C library in Python
[REAPER] How to play with Reascript in Python
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to use Ruby's PyCall to enable pyenv Python
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
How to develop in Python
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
Explain in detail how to make sounds with python
How to deal with pyenv initialization failure in fish 3.1.0
How to run tests in bulk with Python unittest
How to use python interactive mode with git bash
I tried to summarize how to use pandas in python
How to use the model learned in Lobe in Python
How to use classes in Theano
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use virtualenv with PowerShell
Use rospy with virtualenv in Python3
How to get started with Python
[Python] How to use list 3 Added
How to use OpenPose's Python API
How to wrap C in Python
Python: How to use pydub (playback)
How to calculate date with python
Use OpenCV with Python 3 in Window
How to use python zip function
How to handle Japanese in Python
[Python] How to use Typetalk API
How to install Anaconda with pyenv
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
How to use Realsense's Python wrapper PyRealsense with Jetson Nano
How to extract any appointment in Google Calendar with Python