Skip to content

Instantly share code, notes, and snippets.

@startergo
Last active September 20, 2023 15:05
Show Gist options
  • Save startergo/752d9b50cddd66c995add5dd7fb3dc6c to your computer and use it in GitHub Desktop.
Save startergo/752d9b50cddd66c995add5dd7fb3dc6c to your computer and use it in GitHub Desktop.
Python through pyenv
  • As your pyenv command is working but the shims aren't, it most likely means the shell features aren't activated. As of writing, the correct way is to ensure the init command output is evaluated. On macOS, you can add the following to your ~/.bash_profile( ~/.zsh_profile):
eval "$(pyenv init -)"

TL;DR set the env. vars. mentioned in tcl-tk's caveats and this GitHub comment when installing new Pythons via pyenv to get tkinter.

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"

You'll also need to know about pyenv's PYTHON_CONFIGURE_OPTS, --with-tcltk-includes, and --with-tcltk-libs, e.g. from this comment.

Next, reinstall Python with the environment variables active:

Uninstall the current version

export PY_VER=$(python --version | cut -d " " -f 2)
pyenv uninstall $(echo $PY_VER)

Install python3

pyenv install 3.11.5

If python 2.7.18 is installed trough the Python website modify the .zprofile:

image

Test tkinter:

pyenv global 3.11.5
python
Python 3.11.5 (default, Feb 29 2020, 11:56:10)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import tkinter
tkinter.TclVersion, tkinter.TkVersion
(8.6, 8.6)
tkinter._test()

You should get a GUI

If you get the following error, you might be missing the PYTHON_CONFIGURE_OPTS env. var. above.

DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/factor/.pyenv/versions/3.11.5/lib/python3.11/tkinter/__init__.py", line 4552, in _test
    root = Tk()
  File "/Users/factor/.pyenv/versions/3.11.5/lib/python3.11/tkinter/__init__.py", line 2263, in __init__
    self._loadtk()
  File "/Users/factor/.pyenv/versions/3.11.5/lib/python3.11/tkinter/__init__.py", line 2279, in _loadtk
    raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment