希望使用python画散点图时,需要tkinter模块。有些情况下,自行编译安装的python可能并未内置tkinter模块。此时,就会报错:
>>> import tkinter
Traceback (most recent call last):
File "" , 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'
一般来说,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 "" , 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'
Python下"No module named _tkinter"问题解决过程分析
Python升级提示Tkinter模块找不到的解决方法
查询资料后,认为tkinter模块需编译进python的二进制可执行文件中。
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)
重新编译安装python
(a)若使用自动安装工具apt-get或yum,则有可能会自动在python中安装tkinter模块;
(b)若想要在自己编译安装的python(自行编译安装步骤)中加入tkinter模块,则需要进行安装前的配置:
cd Python-3.6.8/
vim Modules/Setup.dist
取消下面几行的注释并修改相应路径:
_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
根据系统中tcl/tk库的路径及头文件位置更改相应的路径:
[dongxw@localhost Python-3.6.8]$ sudo find / -name *tcl.so
[sudo] password for dongxw:
/usr/lib64/libtcl.so
注意:若系统中有多个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 # 应该是该路径
则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
则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
再次参考自行编译安装步骤,编译安装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
>>>