这几天刚刚更新了Python, 从2.4升级到了2.5版本,随后要安装MySQLdb, 结果发现官方网站不提供 python2.5版本下的 windows安装程序。经过一番搜索,找到如下解决方案:
I was able to compile the MySQLDb source 1.2.1_p2 for Pyhton 2.5
using this files:
------------- build1.bat -----------
cl /LD /O2 /W3 /Id:\python25\include /DWIN32 /I"d:\apps\MySQL\MySQL Server 5.0\include" _mysql.c /link /DEF:_mysql.def d:\python25\libs\python25.lib "d:\apps\MySQL\MySQL Server 5.0\lib\opt\mysqlclient.lib" wsock32.lib advapi32.lib
--------------- my_config.h ---------
#define version_info "(1,2,1,'final',2)"
#define __version__ "1.2.1_p2"
--------------- _mysql.def---------
LIBRARY "_mysql"
EXPORTS
init_mysql
------------------------
Rename _mysql.dll to _mysql.pyd
Then copy the stuff (*.py + _mysql.pyd) to lib\site-packages
研究过这个方法之后,发现关键在于 my_config.h 这个文件
再次进入命令行 ,使用 python setup.py build 命令来编译MySQLdb, 得到了一堆连接失败错误
再次检查配置文件, 发现修改文件 site.cfg 文件就可以完成编译。修改后的内容如下:
-------------------------- site.cfg --------------------------------
[options]
embedded = False
threadsafe = True
static = False
# Use the compiler section to add additional options for the extension build.
# In particular, if your platform does not support mysql_config (like
# Windows), you will have to set most of these. Note that each entry is split
# into a list so that each line is one item.
[compiler]
#mysql_root: /usr/local/mysql
library_dirs: e:/mysql/lib/opt
include_dirs: E:/mysql/include
libraries: mysqlclient
zlib
msvcrt
libcmt
wsock32
advapi32
#extra_compile_args:
#extra_objects:
--------------------------------------------------------------------------
其中 mysql_root 这个选项没什么具体用途,后面2个配置项比较重要,注意:这两个目录中不能包含空格