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就可以了。