原文在此
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
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
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.
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問題,我但願這個能夠幫到你