歡迎您光臨本站 註冊首頁

SuSE10使用手記升級python到2.5.1

←手機掃碼閱讀     火星人 @ 2014-03-29 , reply:0

python的最新的版本是2.5.1,從2.4到2.5 有一些語法變化,可以看O』Reilly的OnLamp.com上邊的文章: What』s new in Python2.5? by Jeff Cogwell.

我的python版本是2.4.1,可以使用python ?V來查看版本。並且import Tkinter報錯誤,說_tkinter Module不能找到。所以我就趁這個機會升級到2.5.1好了。

從python.org可以下到Python2.5.1的壓縮包,tar zxvf解壓縮,然後看README,照著它的指導:

./configure 生成makefile文件

make  編譯

sudo make install 安裝

這樣雖然要很久的時間,不過一切都搞完了, python ?V顯示當前版本是2.5.1了。

不過Tkinter卻依舊沒有解決,問題依然是找不到_tkinter模塊。

http://www.python.org/topics/TkInter/有專門的文字來討論這個問題,原因就是python安裝擴展模塊的setup.py沒有發現tcl/tk,所以就沒有編譯Modules/_tkinter.c,而外層的Tkinter實際上是要間接調用這個_tkinter模塊的。

Windows用戶沒有這個問題,因為python的windows安裝版是把Tcl/tk和TkInter直接配置了的。但是一般的linux發行版自帶的python都沒有配置好tcl/tk。

README對安裝Tkinter模塊的說明是:

Tkinter

-------

The setup.py script automatically configures this when it detects a

usable Tcl/Tk installation. This requires Tcl/Tk version 8.0 or

higher.



For more Tkinter information, see the Tkinter Resource page:

http://www.python.org/topics/TkInter/



There are demos in the Demo/tkinter directory.



Note that there's a Python module called "Tkinter" (capital T) which

lives in Lib/lib-tk/Tkinter.py, and a C module called "_tkinter"

(lower case t and leading underscore) which lives in

Modules/_tkinter.c. Demos and normal Tk applications import only the

Python Tkinter module -- only the latter imports the C _tkinter

module. In order to find the C _tkinter module, it must be compiled

and linked into the Python interpreter -- the setup.py script does

this. In order to find the Python Tkinter module, sys.path must be

set correctly -- normal installation takes care of this.



也就是說編譯好python之後,要用setup.py來編譯安裝extension modules,這個setup.py就在Python-2.5.1目錄下。

README讓我確認我已經安裝了Tcl和Tk, Tkinter其實就是Tk interface的意思,它只是tk的介面,我用rpm ?q tcl和rpm ?q tk來查看,我確認我已經安裝了Tcl/tk:

linux:/home/navy/Python-2.5.1 # rpm -q tk

tk-8.4.11-5

linux:/home/navy/Python-2.5.1 # rpm -q tcl

tcl-8.4.11-2

但是make的時候,卻顯示:

running build_ext

INFO: Can't locate Tcl/Tk libs and/or headers



那麼為什麼還不能編譯_tkinter呢?

setup.py文件里有個函數detect_tkinter,用來查找是否條件編譯_tkinter,

def detect_tkinter(self, inc_dirs, lib_dirs):



# Assume we haven't found any of the libraries or include files

# The versions with dots are used on Unix, and the versions without

# dots on Windows, for detection by cygwin.

tcllib = tklib = tcl_includes = tk_includes = None

for version in ['8.5', '85', '8.4', '84', '8.3', '83', '8.2',

'82', '8.1', '81', '8.0', '80']:

tklib = self.compiler.find_library_file(lib_dirs, 'tk' + version)

tcllib = self.compiler.find_library_file(lib_dirs, 'tcl' + version)

if tklib and tcllib:

# Exit the loop when we've found the Tcl/Tk libraries

break

# Now check for the header files

if tklib and tcllib:

# Check for the include files on Debian and {Free,Open}BSD, where

# they're put in /usr/include/{tcl,tk}X.Y



if (tcllib is None or tklib is None or

tcl_includes is None or tk_includes is None):

self.announce("INFO: Can't locate Tcl/Tk libs and/or headers", 2)

return



我只列出一部分代碼,但是很清楚setup.py尋找tcl.h和tk.h兩個頭文件,但是我用find / -name tcl.h在整個linux範圍內都沒有找到這個頭文件。

再仔細看python.org/topics/tkinter/給出的指導:

You may have to install Tcl and Tk(when using RPM, install the ?devel RPM as well) and /or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning 「make」 should build the _tkinter extension.

所以我要安裝tcl-devel和tk-devel才能有頭文件,安裝tcl/tk,只是把靜態或者動態庫考到lib目錄下,只有tcl-devel,tk-devel才會把頭文件放到/usr/include裡邊,而_tkinter要編譯必須找到這些頭文件。

因為我是在SuSE下,在命令行下可以使用yast命令進行package的配置,安裝好了之後,檢查一下:

navy@linux:~/Python-2.5.1> rpm -qa | grep tcl

tcl-devel-8.4.11-2

tcllib-1.7-6

tcl-8.4.11-2

navy@linux:~/Python-2.5.1> rpm -qa | grep ^tk

tkimg-1.3-32

tk-8.4.11-5

tk-devel-8.4.11-5

這樣就可以了,在Python-2.5.1目錄下python setup.py install,就會檢查tcl/tk環境正確,然後開始編譯_tkinter module,安裝到正確的目錄下。這下就可以正確在linux下使用python來寫tk程序了。

並且Python-2.5.1/Misc/下有一個python-mode.el文件,可以把emacs加上python-mode支持,不錯。

升級到2.5.1之後,我發現原來的shelve模塊也不能用了,說 不能找到_bsddb模塊,這個是Berkeley DB,我又照貓畫虎的把db-devel安裝上,再python setup.py install就可以了。

[火星人 ] SuSE10使用手記升級python到2.5.1已經有518次圍觀

http://coctec.com/docs/linux/show-post-193222.html