Neurohazard
暮雲煙月,皓首窮經;森羅萬象,如是我聞。

Python MySQL Driver

wpadmin~August 13, 2018 /System Management

Python MySQL Driver | Python MySQL 驱动

Link

mysql-connector
https://pypi.org/project/mysql-connector/#files

mysql-connector-python
https://pypi.org/project/mysql-connector-python/

MySQLdb (坑很多,不推荐)
MySQL-python
https://pypi.org/project/MySQL-python/

一些坑

遇到的错误
看起来是缺少了一个名为 config-win.h 的文件

C:\Users\Jack\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -IC:\Python27\include -Id:\pydev\venv\vpython2\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:\\Users\\Jack\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2

解决方案

(Windows 10 x64 环境) 步骤可能有冗余

1 【下载】
下载低版本的 MySQL C Driver (6.0.2)
注意! 更高版本的就无效了。
https://downloads.mysql.com/archives/c-c/

mysql-connector-c-6.0.2-winx64.msi
mysql-connector-c-6.0.2-win32.msi

2 【安装】
选择 Custom Install , 将 Extra Binaries 打勾。
安装完成后你会在如下路径找到 config-win.h
C:\Program Files\MySQL\MySQL Connector C 6.0.2\include\config-win.h

3 【拷贝】
观察到在错误信息中,cl.exe 是在 x86 的目录下 include 文件的
"-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include"
所以直接把 Program Files 下的 整个 MySQL Connector C 6.0.2 目录直接拷贝的 Program Files (x86) 下。

C:\Program Files\MySQL\MySQL Connector C 6.0.2
->
C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2

4 【重新安装】
再运行 pip install MySQL-python
总之恰好成功了。

参考资料

1 “Cannot open include file: ‘config-win.h’: No such file or directory” while installing mysql-python
https://stackoverflow.com/questions/1972259/cannot-open-include-file-config-win-h-no-such-file-or-directory-while-inst/16619567

2 mysql-python-install-error-cannot-open-include-file-config-win-h
https://stackoverflow.com/questions/26866147/mysql-python-install-error-cannot-open-include-file-config-win-h

Leave a Reply

Your email address will not be published. Required fields are marked *