英文:
tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
问题
我正在尝试在CentOS-7上通过pyenv安装Python-3.11.4。它安装成功,但没有GUI。我收到以下错误消息:
安装 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'
警告:Python tkinter 扩展未编译,检测到缺少 Tk 工具包?
Python-3.11.4 已安装到 /.../pyenv/versions/3.11.4
而Python-3.9.16在同一台机器上成功安装。根据Python 3.11 Build Changes,要求安装*"Tcl/Tk 版本 8.5.12 或更新版本"*。我已经安装了如下版本:
$ rpm -q tk tk-devel
tk-8.5.13-6.el7.x86_64
tk-devel-8.5.13-6.el7.x86_64
同一页还提到*"Tcl/Tk 和 uuid 标志会被 pkg-config(如果可用)检测到。现在 tkinter 需要 pkg-config 命令来检测 Tcl/Tk 标头和库的开发设置。*,这也已安装:
$ rpm -q pkgconfig
pkgconfig-0.27.1-4.el7.x86_64
请问您能帮助我理解为什么无法安装 _tkinter
吗?非常感谢您的帮助!
英文:
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
tk-8.5.13-6.el7.x86_64
tk-devel-8.5.13-6.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!
答案1
得分: 0
为了在CentOS-7下成功构建Python-3.11.4,需要设置以下环境变量:
export CPPFLAGS="$(pkg-config --cflags openssl11) -I/usr/include"
export LDFLAGS="$(pkg-config --libs openssl11) -L/usr/lib64 -ltcl8.5 -ltk8.5"
其中,openssl11
部分是为了 _ssl
模块,其余部分是为了 _tkinter
模块。
构建带有Tk/Tcl的Python所需的部分可以在以下位置找到:
- /usr/lib64/tclConfig.sh
- /usr/lib64/tkConfig.sh
要检查是否成功构建了 tkinter
,请执行以下操作:
/path/to/python/3.11.4/bin/python3 -m tkinter
看起来问题的根本原因是在CentOS-7中的 tcl
和 tk
rpm包未提供相应的 pkg-config
文件:
- /usr/lib64/pkgconfig/tcl.pc
- /usr/lib64/pkgconfig/tk.pc
因此,需要手动提供相应的信息。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论