在Mac OS X 10.6 Snow Leopard上使用MAMP在Django上安装MySQLdb

所以我知道这不是一个新主题,但它似乎没有人能够解决,至少不适用于
Python 2.6 / Snow Leopard. (我发现的Leopard修复版不适用于Snow Leopard.)

情况:我正试图在我的Mac OS X Snow Leopard笔记本电脑上安装Django. (10.6.7)我有Python 2.6.1,它预装了Snow Leopard,MySQL-python 1.2.3和MAMP 1.9.6.所有都是最新版本.

不对MySQLdb包进行任何更改,如果我运行python setup.py build我会得到数百或更多错误,第一个错误是:

$python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.6-universal-2.6
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/Applications/MAMP/Library/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -fno-omit-frame-pointer -g
_mysql.c:36:23: error: my_config.h: No such file or directory
_mysql.c:38:19: error: mysql.h: No such file or directory
_mysql.c:39:26: error: mysqld_error.h: No such file or directory
_mysql.c:40:20: error: errmsg.h: No such file or directory
_mysql.c:76: error: expected specifier-qualifier-list before 'MYSQL'
_mysql.c:90: error: expected specifier-qualifier-list before 'MYSQL_RES'

并以:结尾

_mysql.c:2422: error: initializer element is not constant
_mysql.c:2422: error: (near initialization for '_mysql_ResultObject_memberlist[0].offset')
_mysql.c: In function '_mysql_ConnectionObject_getattr':
_mysql.c:2444: error: '_mysql_ConnectionObject' has no member named 'open'
lipo: can't open input file: /var/folders/Br/Br8Yhf-IGVCaGfXw4TYRc++++TI/-Tmp-//ccFnIslh.out (No such file or directory)
error: command 'gcc-4.2' failed with exit status 1

所以我使用mysql_config的位置更新了我的site.cfg文件:

# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /Applications/MAMP/Library/bin/mysql_config

还是一样的错误.我花了两天时间进行故障排除,所以我做了很多其他的事情(包括ez_setup,下载.egg文件,并手动更改代码中的一些选项),但没有一个产生任何不同的结果所以我不会厌烦你所有的细节.总的来说,可能会有一些明显我想念的东西,谁知道呢? (希望). Python和MySQL都运行良好,所以我还没有做过一件事,并试图避免重新安装MySQL而不是通过MAMP.但如果有人有理由相信这是必要的,我会试试.

任何帮助将非常感激!谢谢.

最佳答案 我能够按照你的期望让它工作.

几件事:

首先,lipo:无法打开输入文件表示事先编译失败.设置脚本应该失败并显示早期错误;我不知道他们为什么不是.

其次,MySQLdb从mysql_config获取其所有配置信息.因此,例如,执行mysql_config –cflags会给出C编译器标志.在我的情况下,它发出-I /usr/local/Cellar / mysql / 5.5.12 / include -g.在那里没有-arch标志的情况下,错误的架构会成为目标.此外,链接体系结构(在此安装文件中)也是从mysql_config –cflags派生的,因此链接将针对错误的体系结构.

使用ARCHFLAGS显式设置架构可以解决问题.以下脚本假定您使用的MySQL和python构建与uname -m返回的机器架构相匹配;如果没有,请明确指定.

[user]$sudo su
[root]$export ARCHFLAGS="-arch $(uname -m)" # or '-arch i386' or '-arch x86_64'
[root]$pip install mysql-python
点赞