Linux升級Python提示Tkinter模塊找不到解決


一、安裝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模塊

1 [root@li250-193 ~]# yum -y install tkinter
2 ...
3 [root@li250-193 ~]# python
4 Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) 
5 [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
6 Type "help", "copyright", "credits" or "license" for more information.
7 >>> import Tkinter
8 >>>

二、升級Python

Linux的Python版本默認都不叫低

查看Python版本

1 [root@li250-193 ~]# python -V
2 Python 2.6.6

DOWN新版本

1 [root@li250-193 ~]# wget http://www.python.org/ftp/python/2.7.4/Python-2.7.4.tgz

解壓安裝

1 [root@li250-193 ~]# tar -xf Python-2.7.4.tgz 
2 [root@li250-193 ~]# cd Python-2.7.4
3 [root@li250-193 Python-2.7.4]# ./configure
4 ...
5 [root@li250-193 Python-2.7.4]# make
6 ...
7 [root@li250-193 Python-2.7.4]# make install
8 ...

看看新版本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]# find /usr -name *tkinter.so
/usr/lib64/python2.6/lib-dynload/_tkinter.so

找到一個,在2.6舊版本的目錄下,估計是因為新版本庫指向問題。於是認真讀了README說明。重新配置安裝

三、正確安裝新版Python

首先修改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

1
[root@li250-193 Python-2.7.4] # yum -y install tcl-devel tk-devel

開始配置安裝

1
2
3
4
5
6
7
[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模塊

1
2
3
4
5
6
[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
>>>

已經沒問題,舊版再看看

1
2
3
4
5
6
[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命令路徑

1
2
[root@li250-193 Python-2.7.4] # which python
/usr/local/bin/python

cp 過去

1
[root@li250-193 Python-2.7.4] # cp python /usr/local/bin/python

四、升級Python引起yum版本無法使用的問題解決

不少童鞋安裝后就

cp python /usr/bin/python

導致yum時就提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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默認版本,大家的默認版本是多少請按實際情況修改即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM