今天太倒霉了,这篇文章马上就要写完了,结果火狐崩溃了,而且csdn居然没有自动保存,只有上午的有记录;想到这个安装 的问题也折腾哥快一天了,还是再次打字把问题记录下吧,我就喜欢偏向虎山行,不让我记,休想!
之前学习web.py的tutorial 时,练习到数据库时,就没有继续下去,今天反正有空,就顺便想学习下数据库的基本内容,于是安装了mysql,有了下文:
环境:
Win7 32bit +python2.7+MySQL5.5
注意:
python安装中也许已经确认自己安装了某个模块,但是在import时候老是报找不到,那就重启下电脑再import;我已经发现有时候有效。推荐一个很好的已经编译好的模块下载地址:
Python Extension Packages for Windows - Christoph Gohlke
省去对某些编译器的依耐
1、安装mysql
首先到mysql官网下载文件:mysql-installer-community-5.5.27.2.msi 安装过程同一般exe文件,不再叙述,直接下一步即可;
2、安装MySQL-python-1.2.3
直接根据web.py给出 的链接,下载文件MySQL-python-1.2.3.tar.gz 解压到任意目录
然后在cmd命令行行下进行安装;
C:\...> python setup.py install
C:\...> python setup.py bdist_wininst
执行第一句后出现报错如下:
C:\mywork\MySQL_python\MySQL-python-1.2.3>python setup.py install
Traceback (most recent call last):File "setup.py", line 15, in <module>
metadata, options = get_config()
File "C:\mywork\MySQL_python\MySQL-python-1.2.3\setup_windows.py", line 7, in
get_config
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, option s['registry_ke
y'])
WindowsError: [Error 2]
上述报错原因在:
On Windows, you will definitely have to edit site.cfg since there is
no mysql_config in the MySQL package.
问题根源:Mysql-python无法找到所述版本的mysql
在MySQL-python-1.2.3安装包目录下找到site.cfg文件,文件的最后一行中有mysql的版本,更改为自己的版本号即可
如下:
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.5
再次进行安装;
出现如下报错情况:
C:\mywork\MySQL_python\MySQL-python-1.2.3>python setup.py installrunning 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
reading manifest file 'MySQL_python.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
writing manifest file 'MySQL_python.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
creating build
creating build\lib.win32-2.7
copying _mysql_exceptions.py -> build\lib.win32-2.7
creating build\lib.win32-2.7\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win32-2.7\MySQLdb
copying MySQLdb\converters.py -> build\lib.win32-2.7\MySQLdb
copying MySQLdb\connections.py -> build\lib.win32-2.7\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win32-2.7\MySQLdb
copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
copying MySQLdb\times.py -> build\lib.win32-2.7\MySQLdb
creating build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win32-2.7\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.7\MySQLdb\constants
running build_ext
building '_mysql' extension
error: Unable to find vcvarsall.bat
问题根源:缺少C++的编译器
解决办法1:安装Mingw32(Minimalist GNU For Windows)。网上用的比较多,需要搞gcc编译器,不想安装,没有采用此法,详见文章:http://julabs.me/blog/python/unable-to-find-vcvarsall-bat/。error: Unable to find vcvarsall.bat
;在python的lib\distutils目次下,找到distutils.cfg文件(若是没有,就创建一个)写入[build]
compiler=mingw32
这个对vs不适用,没成功;
解决办法2:安装vs2008 express,vs2008的express c++版本的不大,安装很快,很多python包都需要vs2008版本的c++编译器,因此,安装一下还是有必要的。下载地址。一定要选择vs20XX版本的c++编译器安装,不要选错了,安装完毕后即可进行MySQL-python的安装了。
出现如下报错:C:\mywork\MySQL_python\MySQL-python-1.2.3>python setup.py installrunning 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
reading manifest file 'MySQL_python.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
writing manifest file 'MySQL_python.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
running build_ext
building '_mysql' extension
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 "-IC:\Progr
am Files\MySQL\MySQL Server 5.5\include" -IC:\Python27\include -IC:\Python27\PC
/Tc_mysql.c /Fobuild\temp.win32-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(34) : fatal error C1083: 无法打开包括文件 :“config-win.h”: No such fil
e or directory
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2
问题根源:
Mysql5.5版本更新以后,config-win.h文件已经被移位了,被分离到了连接器(Connector)中,例如路径:C:\Program Files\MySQL\Connector C 6.0.2。因此,Mysql-python已经不能正确找到这个文件,因此安装失败
解决办法:
1.首先更改包根目录下site.cfg文件,添加内容:
1 | # Windows connector libs for MySQL. |
2 | connector = C:\Program Files\MySQL\Connector C 6.0 . 2 |
2.接着同样在根目录下更改文件setup_windows.py,找到如下内容:
1 | include_dirs = [ os.path.join(mysql_root, r 'include' ) ] |
并在其后添加:
1 | include_dirs = [ os.path.join(options[ 'connector' ], r 'include' ) ] |
3.另外在:
1 | library_dirs = [ os.path.join(mysql_root, r 'lib\opt' ) ] |
之后添加:
1 | library_dirs = [ os.path.join(options[ 'connector' ], r 'lib\opt' ) ] |
4.最后的文件更改如下:
1 | library_dirs = [ os.path.join(mysql_root, r 'lib\opt' ) ] |
2 | library_dirs = [ os.path.join(options[ 'connector' ], r 'lib\opt' ) ] |
3 | libraries = [ 'kernel32' , 'advapi32' , 'wsock32' , client ] |
4 | include_dirs = [ os.path.join(mysql_root, r 'include' ) ] |
5 | include_dirs = [ os.path.join(options[ 'connector' ], r 'include' ) ] |
6 | extra_compile_args = [ '/Zl' ] |
C:\Program Files\MySQL\Connector C 6.0.2\include\config-win.h(24) : warning C400
5: “_WIN32_WINNT”: 宏重定义
C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\sdkddkver.h(151) :
参见“_WIN32_WINNT”的前一个定义
C:\Program Files\MySQL\Connector C 6.0.2\include\config-win.h(203) : warning C40
05: “SIZEOF_OFF_T”: 宏重定义
c:\python27\include\pyconfig.h(355) : 参见“SIZEOF_OFF_T”的前一个定义
C:\Program Files\MySQL\Connector C 6.0.2\include\config-win.h(301) : warning C40
05: “HAVE_STDDEF_H”: 宏重定义
c:\python27\include\pyconfig.h(673) : 参见“HAVE_STDDEF_H”的前一个定义
_mysql.c(1364) : warning C4018: “<”: 有符号/无符号不匹配
再安装执行:python setup.py bdist_wininst
暂时不管了,看下能不能导出包来
>>> import MySQLdb
C:\Python27\lib\site-packages\mysql_python-1.2.3-py2.7-win32.egg\_mysql.py:3: Us
erWarning: Module _mysql was already imported from C:\Python27\lib\site-packages
\mysql_python-1.2.3-py2.7-win32.egg\_mysql.pyc, but c:\mywork\mysql_python\mysql
-python-1.2.3 is being added to sys.path
>>>
看来工作已经正常;
说明:
此前对以上同样的报错还有很多其它版本的说法和修改方式,进行试验后,觉得以我目前的安装配置环境,还是这样修改划得来,其它的如还出现过以下问题:
raise ValueError(str(list(result.keys()))) ValueError: [u'path'] 据发现是64 位机出现的问题,我居然也出现了,晕 参见: http://bugs.python.org/issue7511
msvc9compiler.py: ValueError when trying to compile with VC Express 修改编译文件的也有;
安装方法参见:http://blog.victorjabur.com/2011/06/05/compiling-python-2-7-modules-on-windows-32-and-64-using-msvc-2008-express/ 写的很详细,参考价值很大;
还有把注册表里VS100COMNTOOLS项改成VS90COMNTOOLS的方法,使用也没有成功!
Mysql-python的制作者估计知道windows下安装有多么的困难,因此,在Windows下有提供exe安装包,下载地址。下载完成后,安装。由于这个安装包只读取注册表下的Python安装目录,因此,安装目标是我们本机的Python环境
最后贴一下参考网站以做备用:
http://cyfloel0516.sinaapp.com/?p=193
Stackoverflow中关于config-win.h丢失的解决 。
vcvarsall.bat丢失的解决办法(mingw32)。
国外某Blog关于在Windows下安装Mysql-python的文章。