Python升级提示Tkinter模块找不到的解决方法
程序员文章站
2023-11-22 12:28:28
一、安装tkinter
在linux中python默认是不安装tkinter模块,
复制代码 代码如下:
[root@li250-193 ~]# python
py...
一、安装tkinter
在linux中python默认是不安装tkinter模块,
复制代码 代码如下:
[root@li250-193 ~]# python
python 2.6.6 (r266:84292, feb 22 2013, 00:00:18)
[gcc 4.4.7 20120313 (red hat 4.4.7-3)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
traceback (most recent call last):
file "<stdin>", line 1, in <module>
importerror: no module named tkinter
>>>
我们安装tkinter模块
复制代码 代码如下:
[root@li250-193 ~]# yum -y install tkinter
...
[root@li250-193 ~]# python
python 2.6.6 (r266:84292, feb 22 2013, 00:00:18)
[gcc 4.4.7 20120313 (red hat 4.4.7-3)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
二、升级python
linux的python版本默认都不叫低
查看python版本
复制代码 代码如下:
[root@li250-193 ~]# python -v
python 2.6.6
down新版本
复制代码 代码如下:
[root@li250-193 ~]# wget http://www.python.org/ftp/python/2.7.4/python-2.7.4.tgz
解压安装
复制代码 代码如下:
[root@li250-193 ~]# tar -xf python-2.7.4.tgz
[root@li250-193 ~]# cd python-2.7.4
[root@li250-193 python-2.7.4]# ./configure
...
[root@li250-193 python-2.7.4]# make
...
[root@li250-193 python-2.7.4]# make install
...
看看新版本python是否可以使用tkinter?
复制代码 代码如下:
[root@li250-193 python-2.7.4]# ./python
python 2.7.4 (default, apr 12 2013, 08:03:09)
[gcc 4.4.6 20120305 (red hat 4.4.6-4)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
traceback (most recent call last):
file "<stdin>", line 1, in <module>
file "/root/python-2.7.4/lib/lib-tk/tkinter.py", line 39, in <module>
import _tkinter # if this fails your python may not be configured for tk
importerror: no module named _tkinter
>>>
提示找不到tkinter模块?看看旧版的是不是正常
复制代码 代码如下:
[root@li250-193 python-2.7.4]# python
python 2.6.6 (r266:84292, feb 22 2013, 00:00:18)
[gcc 4.4.7 20120313 (red hat 4.4.7-3)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
旧版的没问题,难道需要yum install tkinter一次?
复制代码 代码如下:
[root@li250-193 python-2.7.4]# yum install tkinter
loaded plugins: fastestmirror, security
loading mirror speeds from cached hostfile
* base: mirror.team-cymru.org
* extras: mirror.team-cymru.org
* updates: mirror.team-cymru.org
setting up install process
package tkinter-2.6.6-36.el6.x86_64 already installed and latest version
nothing to do
提示已安装,看来不是tkinter的问题,看看tkinter模块在哪里?
复制代码 代码如下:
[root@li250-193 python-2.7.4]# find /usr -name *tkinter.so
/usr/lib64/python2.6/lib-dynload/_tkinter.so
找到一个,在2.6旧版本的目录下,估计是因为新版本库指向问题。于是认真读了readme说明。重新配置安装
三、正确安装新版python
首先修改setup.dist文件
复制代码 代码如下:
[root@li250-193 python-2.7.4]# vim modules/setup.dist
找到下面这几行,把前面的井号去掉打开它
复制代码 代码如下:
_tkinter _tkinter.c tkappinit.c -dwith_appinit \
-l/usr/local/lib \
-i/usr/local/include \
-ltk8.5 -ltcl8.5 \
-lx11
以上第四行
-ltk8.5 -ltcl8.5 默认是 8.2 ,请你系统实际tcl/tk版本修改
复制代码 代码如下:
[root@li250-193 python-2.7.4]# rpm -qa | grep ^tk
tk-8.5.7-5.el6.x86_64
tkinter-2.6.6-36.el6.x86_64
[root@li250-193 python-2.7.4]# rpm -qa | grep ^tcl
tcl-8.5.7-6.el6.x86_64
我系统中装的是8.5,所以这里我改成了8.5
保存退出
安装tck-devel、tk-devel
复制代码 代码如下:
[root@li250-193 python-2.7.4]# yum -y install tcl-devel tk-devel
开始配置安装
复制代码 代码如下:
[root@li250-193 python-2.7.4]# ldconfig
[root@li250-193 python-2.7.4]# ./configure
...
[root@li250-193 python-2.7.4]# make
...
[root@li250-193 python-2.7.4]# make install
...
看下新版python是否可以使用tkinter模块
复制代码 代码如下:
[root@li250-193 python-2.7.4]# ./python
python 2.7.4 (default, apr 12 2013, 08:49:11)
[gcc 4.4.6 20120305 (red hat 4.4.6-4)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
已经没问题,旧版再看看
复制代码 代码如下:
[root@li250-193 python-2.7.4]# /usr/bin/python2.6
python 2.6.6 (r266:84292, feb 22 2013, 00:00:18)
[gcc 4.4.7 20120313 (red hat 4.4.7-3)] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
也没问题
如果直接敲入python -v查看版本是不是最新的,如果不是可以这么干:
which出python命令路径
复制代码 代码如下:
[root@li250-193 python-2.7.4]# which python
/usr/local/bin/python
cp 过去
复制代码 代码如下:
[root@li250-193 python-2.7.4]# cp python /usr/local/bin/python
四、升级python引起yum版本无法使用的问题解决
不少童鞋安装后就
复制代码 代码如下:
cp python /usr/bin/python
导致yum时就提示
复制代码 代码如下:
[root@lee ~]# yum
there was a problem importing one of the python modules
required to run yum. the error leading to this problem was:
no module named yum
please install a package which provides this module, or
verify that the module is installed correctly.
it's possible that the above module doesn't match the
current version of python, which is:
2.7.4 (default, apr 9 2013, 17:12:56)
[gcc 4.4.7 20120313 (red hat 4.4.7-3)]
if you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/faq
[root@lee ~]#
因为yum头部默认制定python脚本的路径就是
复制代码 代码如下:
#! /usr/bin/python
你把旧版的python替换后就是用不了,不知道为何新版python不能被yum识别,目前唯一最好解决的方法就是修改yum头部声明
改成
复制代码 代码如下:
#! /usr/bin/python2.6
即可,这里的python2.6是我centos默认版本,大家的默认版本是多少请按实际情况修改即可