LoginSignup
2
2

More than 1 year has passed since last update.

M1 Mac (Apple Silicon)におけるpyenv環境でのtkinter 8.6へのバージョンアップ方法

Posted at

pyenv環境のPythonにおいてtkinterのバージョン8.5となっている場合,Warningが表示されたりpng画像表示など使用できない機能があるためtkinterを8.6へアップグレードします.

仕様

  • CPU: Apple M1
  • OS: MacOS Big Sur 11.0.1
  • Python: 3.9.1
  • Pyenv: 2.2.0
  • tkinter: 8.6.12 (入れたいバージョン)

方法

Homebrewからtcl-tkをインストールします.
スクリプト言語TclとそのGUIツールキットのTkを元にtkinterは動作していますが,これらが対応していないことが原因となっているためこちらのアップグレードが必要になります.

brew install tcl-tk

下記を実行すると,MacOSにネイティブ対応していないという情報を得ることがで出来,PATHやコンパイラ,package-configの設定を手動で行う必要があることを示されています.

% brew info tcl-tk
tcl-tk: stable 8.6.12 (bottled) [keg-only]
Tool Command Language
https://www.tcl-lang.org
/opt/homebrew/Cellar/tcl-tk/8.6.12 (3,046 files, 52.2MB)
  Poured from bottle on 2021-11-16 at 13:20:01
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/tcl-tk.rbLicense: TCL
==> Dependencies
Required: openssl@1.1 ✔
==> Caveats
tcl-tk is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc

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

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

==> Analytics
install: 238,165 (30 days), 482,704 (90 days), 2,824,670 (365 days)
install-on-request: 42,120 (30 days), 62,766 (90 days), 196,835 (365 days)
build-error: 65 (30 days)

既に入っているPython 3.9.1をアンインストールし,上記の設定に加えて,CFLAGSとPYTHON_CONFIGURE_OPTSの条件を追加して下記のようにenvを指定してPython 3.9.1を再度インストールします.
このenvでは,tcl-tk v8.6を使ってPython 3.9.1をbuildするよう設定していることになります.

pyenv uninstall 3.9.1
env \
  PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
  LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
  CPPFLAGS="-I$(brew --prefix tcl-tk)/include" \
  PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
  CFLAGS="-I$(brew --prefix tcl-tk)/include" \
  PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'" \
  pyenv install 3.9.1

Pyenvのバージョンを切り替えてtkinterが正しくバージョンが上げられているかを確認します.

pyenv local 3.9.1
% python
Python 3.9.1 (default, Nov 16 2021, 13:22:39) 
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.TclVersion, tkinter.TkVersion
(8.6, 8.6)

以下のコードでも確認してみましたが,正常に8.6へバージョンを上げることができています.

python -m tkinter

スクリーンショット 2021-11-16 13.52.11.png

備考

今回私はM1 Macで設定を行いました.
過去の記事などを参考にしてみるとPython 3.8系を使用しており,これらはM1 Macではネイティブ対応していないため今回はネイティブ対応しているPython 3.9.1を使用しました.

私は上記のenvと以下の実行をなんとか組み合わせて3.8系のインストールを実行しようとしましたが,結果としてtkinter実行時にRuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)と表示されてしまいうまくバージョンを上げることができませんでした.

  arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.8.7

検証不足ではありますが,Tkitnerを指定するためのenvとhomebrewを指定するためのenvを組み合わせるとPATH指定が複数になってしまうため,正しくインストールできないのではと考えています.

参考

https://qiita.com/saki-engineering/items/92b7ec12ed07338929a3
https://stackoverflow.com/questions/60469202/unable-to-install-tkinter-with-pyenv-pythons-on-macos

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2