为64位Windows7的Python3安装MySQLdb

Install 64-bit MySQLdb for Python 3 on Windows 7

为64位Windows7的Pyhton3安装MySQLdb

原文在此
Jesus Rollerblading Christ. It all started as a simple idea: I would install Django and start working on this web app idea I had and use it as a chance to learn more about Python and Django. I also decided to go with MySQL as the database for it since I already have a MySQL server installed on my home computer from previous projects. Little did I know that since I had chosen to go with Python 3, I was in for several evenings of pain and agony dealing with MySQLdb. After an emotional rollercoaster of build path fuckery that made me want to claw my eyes out, I finally got it working: I can use MySQLdb for Python 3.2 on Windows 7 64-bit. Here’s my tale in case you find yourself in the same situation and begin to ask yourself, “Why the fuck do people put up with this?”php

基督尼玛个耶稣.这一切始于一个简单的想法:我想装Django而后写个网站,并用这个机会多学点Python和Djiango的知识.我理所固然的选MySQL当数据库,由于在我家里的那台电脑上装着以前的一个项目里用到的MySQL服务.我从没想到由于选择Python3,让我接下来的几个夜晚都由于处理MySQLdb的问题而过的痛苦.以后的没用的构建路径让我情绪很不稳定,都想抓出本身的眼睛.最后总算是搞定了:我能够在windows 7 64位的Python3中使用MySQLdb了.这就是个人故事,省得你和我踏进赞成个坑而后抬头问苍天:"真的有人能搞定这种操蛋事?"html

The Setup

安装

Like I said, this is all because I was stupid enough to try to install Django 1.5 on Windows 7 using 64-bit Python 3.2 against a MySQL database server. FUCKIN’ N00B, HOW DARE YOU!
就像我刚刚说的,这一切都是由于我特傻叉想在windows 7 64位下,Python 3.2中安装Django 1.5 而后使用MySQL数据库.你也想这么干?真有胆子啊小菜鸟.python

The first warning should’ve been that in Django’s docs they explicitly mention that the latest release of MySQLdb doesn’t support Python 3. “No problem”, I thought, “I’ve made a living off of dealing with software build bullshit, I can handle this.”
第一个警告来自Django官方文档,明确说起最新版的MySQLdb不支持Python 3.(译注:本文发表于2013年6月,直到2016年的今天MySQLdb依然不支持Py3),"没问题",我想,"我就算干这行的,我能搞定"mysql

Getting Started

准备开始

So I had to download a few things to get going:
因此我必须得下几个东西来搞定这事git

1.First I double checked the version of the MySQL server on my computer (which I had installed via Zend Server some time ago). It’s 5.1.43, so then I went hunting on MySQL’s website for a full binary + headers + libs package and found it on http://downloads.mysql.com/ar... So I downloaded the 64-bit zip (I figured since I have 64-bit Python, but it probably didn’t matter). Then I just unpacked that zip to my C:\github

1.首先我检查了我电脑上的MySQL版本(经过Zend Server装的MySQL已经有一段时间了),是5.1.43,我去MySQL官网上找二进制包和连接库,找到了这个 http://downloads.mysql.com/ar...译注,新连接应该是http://dev.mysql.com/download...因此我下载了[这个]3(我想由于我用的是64位python,可是那并不重要),把它解压到C盘web

2.Next I downloaded the MySQL-for-Python-3 package the good Django folks suggested in their docs and unpacked that to a separate folder on C:\sql

2.接下来,下载MySQL-for-Python-3,Django官方建议的包,解压到C盘的另外一个文件夹数据库

3.Also, I already have Visual Studio 2012 installed, so I had all the C compilers and shit at my disposal. Yay.django

3.我已经安装了VS2012,C编译器的问题就搞定了.Yay.

The Blood and Guts

血雨腥风

After much trial and error and googling. This is what I came up with to make it work:
在不断的GOOGLE和试验后,下面是我总结出来的方法:

1.Open up a cmd prompt
1.打开CMD

2.Since I have Visual Studio 2012 I had to trick their (not sure if it’s Python’s or MySQLdb’s) stupid build process by pointing them to my Visual Studio 2012 toolset: SET VS90COMNTOOLS=%VS110COMNTOOLS%
2.由于我装的是VS2012 ,因此我不得不伪造编译路径到个人VS2012命令行SET VS90COMNTOOLS=%VS110COMNTOOLS%

3.Then I cd’ed into the MySQL-for-Python-3 directory I unpacked earlier and edited the site.cfg (not in notepad..) file to change the mysql_location to point to the directory I unpacked the MySQL binary+header+libs package to earlier (e.g. mysql_location = C:mysql-5.1.43-winx64)
3.而后cd进入以前解压的MySQL-for-Python-3的文件夹,编辑site.cfg(不要在记事本中编辑..)文件,更改mysql_location指向以前解压的MySQL二进制包和连接库的文件夹(例如:mysql_location = C:mysql-5.1.43-winx64)

4.Next, I had to edit the _mysql.c file (again, not in notepad) in MySQL-for-Python-3 to change line 35 from my_config.h to config-win.h
4.接下来,编辑_mysql.c文件(同理,不要在记事本中编辑),把35行的my_config.h改为config-win.h

5.Then, I edited my Python distribution’s C:Python32Libdistutilsmsvc9compiler.py to add a line after line 638 (ld_args.append(‘/MANIFESTFILE:’ + temp_manifest)) as: ld_args.append(‘/MANIFEST’). Why? Cause this dude said so, and it worked.
5.继续,编辑Python3目录下Libdistutilsmsvc9compiler.py文件,在638行(译注:在python3.5中是673行,具体视状况而定)后面增长以下内容(ld_args.append(‘/MANIFESTFILE:’ + temp_manifest))

6.Oh yeah, and I had to edit the MySQL-for-Python-3 file setup_windows.py to change ‘lib’ to ‘lib/opt’ on line 17’s setting of library_dirs cause otherwise it couldn’t find the goddamn MySQL libs.
6.最后,编辑 MySQL-for-Python-3里的setup_windows.py文件,把17行处(位置依然视状况而定)library_dirs里的lib改为lib/opt,由于其余地方找不到该死的MySQL连接库

And then FINALLY I was able to build and install the thing:
最后作的就是编译和安装的工做了:

C:MySQL-for-Python-3-master>python setup.py build
running build
running build_py
copying MySQLdbrelease.py -> buildlib.win-amd64-3.2MySQLdb
running build_ext

C:MySQL-for-Python-3-master>python setup.py install

图片描述
And it fucking worked!
终于他喵的能用了!

So yeah, I’m sure some enlightened Python god will tell me I brought this pain upon myself given my version choices (and please tell me how stupid I am for using Windows), but it’s crap like this that makes me want to run screaming and crying back into Visual Studio/C#’s warm embrace, or at least PHP’s bony grasp…
好吧,我知道一些牛逼的Python大神会告诉我你这是自食其果,选这种版原本搞(还请告诉我这笨蛋到底要怎么用Windows),但那些都是让我哭着回到Visual Studio/C#的温暖怀抱或者让我去学PHP皮毛的废话罢了

Anyway, if you’re running into similar issues trying to force MySQLdb’s square peg into Python 3’s round hole, I hope this helps.总之,若是你遇到相似的在python3用MySQLdb问题,我但愿这个能够帮到你

相关文章
相关标签/搜索