I couldn't use tkinter with python installed by pyenv of anyenv

If you search for this symptom, you will find some articles in Qiita, but for some reason it didn't work and I made a lot of trial and error, so I made a note.

TL;DR

--If there is no tcl-tk, enter it with brew install tcl-tk --If you wrote PYTHON_CONFIGURE_OPTS and it doesn't improve, check the version of tcl-tk to see if the correct version is written. -↑ If that doesn't work, try adding --enable-shared to the end.

environment

Mac Book Pro macOS Catalina 10.15.7

anyenv anyenv 1.1.2-1-g67d402f pyenv 1.2.21

What was happening?

Tkinter, which I see in other articles, doesn't work. I didn't make a note of the terminal log because I thought it could be solved quickly, but it was almost the same as I see in other articles.

What I tried

Install tcl-tk with Homebrew

brew install tcl-tk

So, I added various things to the environment variables.

.zshrc


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"

In addition, add the option PYTHON_CONFIGURE_OPTS when building python to .zshrc, At this time, the version of tcl-tk needs to match the version of the one you put in Homebrew, so check it. It seems that you can see the version of the package you put in Homebrew with brew info (I learned today)

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-11-09 at 10:51:55
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/tcl-tk.rb
License: TCL
==> Dependencies
Required: [email protected] ✔
==> 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"' >> ~/.zshrc

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,020 (30 days), 24,799 (90 days), 101,382 (365 days)
install-on-request: 4,416 (30 days), 13,662 (90 days), 53,123 (365 days)
build-error: 0 (30 days)

It seems to be 8.6.10, so in this case, it seems that you should specify tcl8.6 and tk8.6.

(I took a look)

ls /usr/local/opt/tcl-tk/lib
Tk.icns           libtk8.6.dylib    tclConfig.sh      tdbcmysql1.1.1
Tk.tiff           libtkstub8.6.a    tcllib1.20        tdbcodbc1.1.1
itcl4.2.0         pkgconfig         tcllibc           tdbcpostgres1.1.1
itk4.1.0          sqlite3.30.1.2    tclooConfig.sh    thread2.8.5
libtcl8.6.dylib   tcl8              tcltls1.7.20      tk8.6
libtclstub8.6.a   tcl8.6            tdbc1.1.1         tkConfig.sh

So I will add it to .zshrc.

.zshrc


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'"

Let's read the settings again. You can use . ~ / .Zshrc, source ~ / .zshrc, or reopen the terminal in any way you like.

Once that's done, reinstall python. ... Well, since I left it for a long time, the version included was 3.7.4, which is old, so I'm thinking of installing 3.8.5 newly here. (If it goes well, I feel like putting it back in)

pyenv install 3.8.5
python-build: use [email protected] 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

Then switch the global version

pyenv global 3.8.5
python -V
Python 3.8.5

Well then.

python -m tkinter -c "tkinter._test()"
(Omitted)
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)

What?

Everyone says it worked.

Search further

A further search on Google revealed that when setting PYTHON_CONFIGURE_OPTS, adding --enable-shared also worked opinion. / questions / 60469202 / enable-to-install-tkinter-with-pyenv-pythons-on-macos # comment112082121_61879759) So let's try it.

- 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'"
+ 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' --enable-shared"

So, reload .zshrc again and reinstall python

pyenv uninstall 3.8.5
pyenv: remove /Users/*****/.anyenv/envs/pyenv/versions/3.8.5? [y|N]y
pyenv: 3.8.5 uninstalled
pyenv install 3.8.5
python-build: use [email protected] 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

Somehow, it is better to hit idle and see that there is no error, so I will try that as well.

idle

image.png

Looks okay.

Then again.

python -m tkinter -c "tkinter._test()"

image.png

It's finally a success. Yay!

Summary

If you can't use tkinter with the well-known method, you may want to add --enable-shared to your build options.

reference

Recommended Posts

I couldn't use tkinter with python installed by pyenv of anyenv
Use python installed with Pyenv with Sublime REPL of Sublime Text 3
Use multiple versions of python environment with pyenv
How to use tkinter with python in pyenv
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Use Python installed with pyenv for PL / Python execution environment
Why can I use the module by importing with python?
Use Python in pyenv with NeoVim
Use smbus with python3 under pyenv environment
I installed and used Numba with Python3.5
Use Anaconda-Python installed with pyenv from openFrameworks
I tried hundreds of millions of SQLite with python
I want to use MATLAB feval with python
pyenv doesn't work with tcl-tk installed by homebrew.
I wanted to install Python 3.4.3 with Homebrew + pyenv
[Python] I made a Youtube Downloader with Tkinter.
Make OpenCV3 available from python3 installed with pyenv
I want to use Temporary Directory with Python2
python development environment -use of pyenv and virtualenv-
PIL cannot be installed by virtualenv with pyenv
I tried to make a simple mail sending application with tkinter of Python
I want to use Python in the environment of pyenv + pipenv on Windows 10
[Python] Use JSON with Python
Basics of I / O screen using tkinter in python3
I tried to summarize how to use matplotlib of python
String manipulation with python & pandas that I often use
3 things I noticed by analyzing twitter followers with python
[OpenCV / Python] I tried image analysis of cells with OpenCV
Use mecab with Python3
Use DynamoDB with Python
Solved the problem that MacVim installed by Homebrew was not built by python of pyenv
Use Python 3.8 with Anaconda
Use python with docker
Install python with pyenv
Install the latest Python from pyenv installed with homebrew
Memo of "Cython-Speeding up Python by fusing with C"
Version control of Node, Ruby and Python with anyenv
I tried "morphology conversion" of images with Python + OpenCV
A memorandum (masOS) that import tkinter could not be done with python installed from pyenv
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I made a simple typing game with tkinter in Python
I want to specify another version of Python with pyvenv
I wrote the basic grammar of Python with Jupyter Lab
I tried running Movidius NCS with python of Raspberry Pi3
I evaluated the strategy of stock system trading with Python.
What I learned by solving 30 questions of python Project Euler
[Python] I want to use the -h option with argparse
Mass generation of QR code with character display by Python
I made a puzzle game (like) with Tkinter in Python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried a stochastic simulation of a bingo game with Python
I tried fp-growth with python
Programming with Python and Tkinter
I tried scraping with Python
Use Trello API with python
Use Twitter API with Python
I made blackjack with python!
Use TUN / TAP with Python
Reinstall python with pyenv with -fPIC
Run Label with tkinter [Python]