Search code examples
pythontkinterbuildtk-toolkit

tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?


I am trying to install (through pyenv) Python-3.11.4 under CentOS-7. It installs but without GUI. I get the following error message:

Installing Python-3.11.4...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/.../pyenv/versions/3.11.4/lib/python3.11/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Installed Python-3.11.4 to /.../pyenv/versions/3.11.4

While Python-3.9.16 installs successfully on the same machine. According to Python 3.11 Build Changes, the requirement is to have "Tcl/Tk version 8.5.12 or newer" installed. I have

$ rpm -q tk tk-devel tcl tcl-devel
tk-8.5.13-6.el7.x86_64
tk-devel-8.5.13-6.el7.x86_64
tcl-8.5.13-8.el7.x86_64
tcl-devel-8.5.13-8.el7.x86_64

The same page says "Tcl/Tk, and uuid flags are detected by pkg-config (when available). tkinter now requires a pkg-config command to detect development settings for Tcl/Tk headers and libraries.", which is also installed:

$ rpm -q pkgconfig
pkgconfig-0.27.1-4.el7.x86_64

Could you please help me to understand what might be the reason of the failure to install _tkinter?

Thank you very much for your help!


Solution

  • To build successfully Python-3.11.4 under CentOS-7, one needs to set the following environment variables:

    export CPPFLAGS="$(pkg-config --cflags openssl11) -I/usr/include"
    export LDFLAGS="$(pkg-config --libs openssl11) -L/usr/lib64 -ltcl8.5 -ltk8.5"
    

    where the openssl11 parts are needed for the _ssl module, and the rest is needed for the _tkinter module.

    The pieces needed to build Python with the Tk/Tcl were found in

    • /usr/lib64/tclConfig.sh
    • /usr/lib64/tkConfig.sh

    To check if tkinter was built successfully, do the following:

    /path/to/python/3.11.4/bin/python3 -m tkinter
    

    It seems, the root cause of the problem is that tcl and tk rpm packages in CentOS-7 do not provide the corresponding pkg-config files:

    • /usr/lib64/pkgconfig/tcl.pc
    • /usr/lib64/pkgconfig/tk.pc

    and so one has to provide the corresponding information manually.