centos7中Python项目缺少tkInter

在centos7上部署一个python项目,此项目需要用到tkinter,但是已经安装了,导入的时候提示缺少tkinter,最终在下文链接文章中找到方法解决,特此记录
原文链接:https://blog.csdn.net/weixin_44992607/article/details/122123679

【故事背景】
希望使用python画散点图时,需要tkinter模块。有些情况下,自行编译安装的python可能并未内置tkinter模块。此时,就会报错:

>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dongxw/usr/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
1
2
3
4
5
6
一般来说,yum安装的tkinter会位于系统目录中。仅仅使用PYTHONPATH环境变量或sys.path.append()引入tkinter的库文件的路径,无法在python中成功导入tkinter模块:

>>> sys.path.append('/usr/lib64/python3.6/tkinter')
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dongxw/usr/lib/python3.6/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
1
2
3
4
5
6
7
Python下"No module named _tkinter"问题解决过程分析
Python升级提示Tkinter模块找不到的解决方法
查询资料后,认为tkinter模块需编译进python的二进制可执行文件中。

【python中加入tkinter模块的编译安装】
相关软件介绍:
Tcl & Tk: Tcl 是“工具控制语言(Tool Command Language)”的缩写,其面向对象为otcl语言。Tk 是 Tcl“图形工具箱”的扩展,它提供各种标准的 GUI 接口项,以利于迅速进行高级应用程序开发。

python3-tk: tkinter是一个python的接口类库,用以调用tcl/tk程序,故一般在操作系统层面会有相应的类库安装,而非仅仅依靠pip3来安装相应的python类库,比如, python3-tk。

tk-devel: 在*unix系统中,在进行开发之中,很多情况下是需要devel类库安装的,这个是一个大概率的规则。

安装步骤:
先安装tk和tcl,python3-tk、tk-devel

sudo apt install python3-tk  (Ubuntu)   # 安装python3-tk
yum install python3-tkinter    (Centos)

sudo apt install tk-dev  (Ubuntu)   # 安装tk开发类库
yum install tk-devel    (Centos)
1
2
3
4
5
重新编译安装python
(a)若使用自动安装工具apt-get或yum,则有可能会自动在python中安装tkinter模块;
(b)若想要在自己编译安装的python(自行编译安装步骤)中加入tkinter模块,则需要进行安装前的配置:

cd Python-3.6.8/
vim Modules/Setup.dist
1
2
取消下面几行的注释并修改相应路径:

_tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
# *** Uncomment and edit to reflect where your Tcl/Tk libraries are:
    -L/usr/lib64\
# *** Uncomment and edit to reflect where your Tcl/Tk headers are:
    -I/usr/include \
# *** Uncomment and edit to reflect your Tcl/Tk versions:
    -ltk8.5 -ltcl8.5 \
# *** Always uncomment this; X11 libraries to link with:
    -lX11
1
2
3
4
5
6
7
8
9
根据系统中tcl/tk库的路径及头文件位置更改相应的路径:

[dongxw@localhost Python-3.6.8]$ sudo find / -name *tcl.so
[sudo] password for dongxw: 
/usr/lib64/libtcl.so
1
2
3
注意:若系统中有多个tcl.h文件,例如:

[dongxw@localhost Python-3.6.8]$ sudo find / -name *tcl.h
[sudo] password for dongxw: 
/home/dongxw/usr/include/tcl.h
/home/dongxw/usr/include/itcl.h
/usr/include/tcl-private/generic/tcl.h
/usr/include/tcl.h      # 应该是该路径
1
2
3
4
5
6
则make时会报错类似
/usr/include/tk.h:21:3: error: #error Tk 8.5 must be compiled with tcl.h from Tcl 8.5
# error Tk 8.5 must be compiled with tcl.h from Tcl 8.5
/usr/include/tcl.h中显示确实是8.5版本,但是/home/dongxw/usr/include/tcl.h中显示是8.6版本,怀疑是其造成了tk.h读取tcl版本的影响。将其更名为/home/dongxw/usr/include/tcl.h.bak,

[dongxw@localhost Python-3.6.8]$ sudo find / -name *tcl.h
/home/dongxw/usr/include/itcl.h
/usr/include/tcl-private/generic/tcl.h
/usr/include/tcl.h
1
2
3
4
则make时不会再报错。
参考:记录本人与Xshell, X11, Xming, CentOS, Tk, Tkinter, Matplotlib相关的坑

-ltk8.5 -ltcl8.5 默认是 8.2 ,需要按照系统实际tcl/tk版本修改

[dongxw@localhost Python-3.6.8]$ rpm -qa|grep tcl
targetcli-2.1.51-2.el7.noarch
tcl-8.5.13-8.el7.x86_64
tcl-devel-8.5.13-8.el7.x86_64
1
2
3
4
再次参考自行编译安装步骤,编译安装python,则可成功引入tkinter模块:

[dongxw@localhost ~]$ /home/dongxw/usr/bin/python3
Python 3.6.8 (default, Dec 23 2021, 17:06:56) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容