资讯专栏INFORMATION COLUMN

为64位Windows7的Python3安装MySQLdb

Corwien / 2740人阅读

摘要:为位的安装原文在此基督尼玛个耶稣这一切始于一个简单的想法我想装然后写个网站并用这个机会多学点和的知识我理所当然的选当数据库因为在我家里的那台电脑上装着之前的一个项目里用到的服务我从没想到因为选择让我接下来的几个夜晚都因为处理的问题而过的

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?”

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

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数据库.你也想这么干?真有胆子啊小菜鸟.

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),"没问题",我想,"我就算干这行的,我能搞定"

Getting Started 准备开始

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

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:

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

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:

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.

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问题,我希望这个可以帮到你

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38512.html

相关文章

  • Windows64+Python3+selenium3自动化环境搭建(1)

    摘要:若不出现下方界面则跳过此步启动后,错误提示丢失。处理方法下载安装运行库即可。调出命令窗口并输入出现下图显示内容则表示版本安装成功。将放在盘中文件夹下的,如果是位系统则放在中四打开检验环境是否搭建成功出现下方界面则表示搭建成功 一、Python安装1、Python3官网下载https://www.python.org/downlo... 2、选择对应系统和版本(注意是32位还是64位,我...

    张迁 评论0 收藏0
  • Windows Python3环境 pip安装 mysqlclient包

    摘要:环境下安装包经常会有各种环境方面的问题及报错。所以此次报错的原因就出来了,缺少了这个头文件。所以其核心原因在于环境的依赖库中不存在相关的文件。其它在上有网友提供了一个较为省事儿的解决办法,直接下载现成的文件到本地进行安装资源地址。 Windows 环境下安装python包经常会有各种环境方面的问题及报错。 最近在python36/37下升级安装mysqlclient包时产生了下面的错误...

    cikenerd 评论0 收藏0
  • 64windows7安装apache2.4+php7.1+mysql5.7+apcu

    摘要:表示需要运行库,缺少它将会在接来下的过程中弹出类似的提示运行库下载如果以前安装过,则不必再装。回车回车回车即可将服务创建,关闭窗口。下的安装有一个临时密码问题。有空再把下的安装记录一下。 最近想更新Web服务器上的软件,查了一下apache、php、mysql版本都很高了,有些变动还很大,所以先在Win上安装熟悉一下,下面是安装配置记录: 系统:64位Windows7时间:2017年3...

    pingink 评论0 收藏0

发表评论

0条评论

Corwien

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<