## 1、下载MySQLdb
[去官网](http://pypi.python.org/pypi/MySQL-python/)
下载对应的编译好的版本(现在官网最新版本为1.2.5):
MySQL-python-1.2.5.win32-py2.7.exe
得到1MB的安装文件
MySQL-python-1.2.5.win32-py2.7.exe
## 2、安装
以上版本目前只支持到python2.7,在安装MySQLdb之前确定你已经在电脑上安装了python27。
双击安装:
然后点下一步出现:
我确实已经安装了python 2.7
究其原因是因为此x86的exe,是不支持我的win7 x64。
## 3、源码安装
[下载源码] (https://pypi.python.org/pypi/MySQL-python/1.2.5)
然后再去解压和安装,结果果然出现错误:
error: Unable to find vcvarsall.bat
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.5\MySQL-python-1.2.4>setup.py install
Extracting in c:\users\cli\appdata\local\temp\tmpkapo0l
Now working in c:\users\cli\appdata\local\temp\tmpkapo0l\distribute-0.6.28
Building a Distribute egg in D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4\distribute-0.6.28-py2.7.egg
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info\PKG-INFO
writing top-level names to MySQL_python.egg-info\top_level.txt
writing dependency_links to MySQL_python.egg-info\dependency_links.txt
writing MySQL_python.egg-info\PKG-INFO
writing top-level names to MySQL_python.egg-info\top_level.txt
writing dependency_links to MySQL_python.egg-info\dependency_links.txt
reading manifest file ‘MySQL_python.egg-info\SOURCES.txt’
reading manifest template ‘MANIFEST.in’
writing manifest file ‘MySQL_python.egg-info\SOURCES.txt’
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib.win-amd64-2.7
copying _mysql_exceptions.py -> build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb__init__.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-2.7\MySQLdb
creating build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants__init__.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-2.7\MySQLdb\constants
running build_ext
building ‘_mysql’ extension
error: Unable to find vcvarsall.bat
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4>
然后参考之前的办法,结果还是出错:
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4>SET VS90COMNTOOLS=%VS100COMNTOOLS%
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4>setup.py install
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info\PKG-INFO
writing top-level names to MySQL_python.egg-info\top_level.txt
writing dependency_links to MySQL_python.egg-info\dependency_links.txt
writing MySQL_python.egg-info\PKG-INFO
writing top-level names to MySQL_python.egg-info\top_level.txt
writing dependency_links to MySQL_python.egg-info\dependency_links.txt
reading manifest file ‘MySQL_python.egg-info\SOURCES.txt’
reading manifest template ‘MANIFEST.in’
writing manifest file ‘MySQL_python.egg-info\SOURCES.txt’
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
running build_ext
building ‘_mysql’ extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,4,’final’,1) -D__version__=1.2.4 “-IC:\Program Files (x86)\MySQL\MySQL
Connector C 6.0.2\include” -ID:\tmp\dev_install_root\Python27_x64\include -ID:\tmp\dev_install_root\Python27_x64\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release_mysql.obj /Zl
_mysql.c
_mysql.c(42) : fatal error C1083: Cannot open include file: ‘config-win.h’: No such file or directory
error: command ‘”C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\cl.exe”’ failed with exit status 2
D:\tmp\dev_tools\python\mysql\MySQL-python-1.2.4\MySQL-python-1.2.4>
然后再参考:
Windows下安装MySQLdb遇到的问题及解决方法
所说的:
1)问题 : _mysql.c(34) : fatal error C1083: Cannot open include file: ‘config-win.h’: No such file or directory
error: command ‘”C:/Program Files/Microsoft Visual Studio 9.0/VC/BIN/cl.exe”‘ failed with exit status 2
原因 :原因是安装MySQL的时候没有安装C语言库。
解决 :重新运行MySQL的安装程序,选择Modify,把“C Include Files / Lib Files”勾选上,并安装。