centos7中Python項(xiàng)目缺少tkInter

在centos7上部署一個(gè)python項(xiàng)目,此項(xiàng)目需要用到tkinter,但是已經(jīng)安裝了,導(dǎo)入的時(shí)候提示缺少tkinter,最終在下文鏈接文章中找到方法解決,特此記錄
原文鏈接:https://blog.csdn.net/weixin_44992607/article/details/122123679

【故事背景】
希望使用python畫(huà)散點(diǎn)圖時(shí),需要tkinter模塊。有些情況下,自行編譯安裝的python可能并未內(nèi)置tkinter模塊。此時(shí),就會(huì)報(bào)錯(cuò):

>>> 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
一般來(lái)說(shuō),yum安裝的tkinter會(huì)位于系統(tǒng)目錄中。僅僅使用PYTHONPATH環(huán)境變量或sys.path.append()引入tkinter的庫(kù)文件的路徑,無(wú)法在python中成功導(dǎo)入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"問(wèn)題解決過(guò)程分析
Python升級(jí)提示Tkinter模塊找不到的解決方法
查詢資料后,認(rèn)為tkinter模塊需編譯進(jìn)python的二進(jìn)制可執(zhí)行文件中。

【python中加入tkinter模塊的編譯安裝】
相關(guān)軟件介紹:
Tcl & Tk: Tcl 是“工具控制語(yǔ)言(Tool Command Language)”的縮寫(xiě),其面向?qū)ο鬄閛tcl語(yǔ)言。Tk 是 Tcl“圖形工具箱”的擴(kuò)展,它提供各種標(biāo)準(zhǔn)的 GUI 接口項(xiàng),以利于迅速進(jìn)行高級(jí)應(yīng)用程序開(kāi)發(fā)。

python3-tk: tkinter是一個(gè)python的接口類(lèi)庫(kù),用以調(diào)用tcl/tk程序,故一般在操作系統(tǒng)層面會(huì)有相應(yīng)的類(lèi)庫(kù)安裝,而非僅僅依靠pip3來(lái)安裝相應(yīng)的python類(lèi)庫(kù),比如, python3-tk。

tk-devel: 在*unix系統(tǒng)中,在進(jìn)行開(kāi)發(fā)之中,很多情況下是需要devel類(lèi)庫(kù)安裝的,這個(gè)是一個(gè)大概率的規(guī)則。

安裝步驟:
先安裝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開(kāi)發(fā)類(lèi)庫(kù)
yum install tk-devel    (Centos)
1
2
3
4
5
重新編譯安裝python
(a)若使用自動(dòng)安裝工具apt-get或yum,則有可能會(huì)自動(dòng)在python中安裝tkinter模塊;
(b)若想要在自己編譯安裝的python(自行編譯安裝步驟)中加入tkinter模塊,則需要進(jìn)行安裝前的配置:

cd Python-3.6.8/
vim Modules/Setup.dist
1
2
取消下面幾行的注釋并修改相應(yīng)路徑:

_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
根據(jù)系統(tǒng)中tcl/tk庫(kù)的路徑及頭文件位置更改相應(yīng)的路徑:

[dongxw@localhost Python-3.6.8]$ sudo find / -name *tcl.so
[sudo] password for dongxw: 
/usr/lib64/libtcl.so
1
2
3
注意:若系統(tǒng)中有多個(gè)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      # 應(yīng)該是該路徑
1
2
3
4
5
6
則make時(shí)會(huì)報(bào)錯(cuò)類(lèi)似
/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中顯示確實(shí)是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時(shí)不會(huì)再報(bào)錯(cuò)。
參考:記錄本人與Xshell, X11, Xming, CentOS, Tk, Tkinter, Matplotlib相關(guān)的坑

-ltk8.5 -ltcl8.5 默認(rèn)是 8.2 ,需要按照系統(tǒng)實(shí)際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
>>> 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容