Mac MySQLdb模块安装,可算解决了

时间:2023-03-09 03:52:54
Mac MySQLdb模块安装,可算解决了

转载:http://blog.****.net/janronehoo/article/details/25207825

叨叨

短评:这篇文章感觉是比较全面解决Mac MySQLdb模块安装问题的文章了,特别转载一下

安装过程主要是mysql_config not found错误,这个错误通常卡住很多初学者,以及安装后出现的 image not found 错误

版本:Python 2.7.3

MySQL-python包中,因此无论下载还是在pip中search,都应该是搜寻MySQL-python。

流程:

下载MySQLdb

MySQL-python-1.2.4b4.tar,下载后解压,然后在终端Terminal中执行以下命令:

new-host-:~ iFantastic$ cd /Users/iFantastic/Downloads/MySQL-python-1.2.4b4
new-host-:MySQL-python-1.2.4b4 iFantastic$ python setup.py install

使用PIP安装MySQLdb

new-host-:~ iFantastic$ pip install MySQL-python

无论是在线安装还是下载安装,此时你可能会遇到第一个错误提示:

EnvironmentError: mysql_config not found

解决mysql_config not found错误

因此下载安装时的解决办法为:在MySQL-python的安装包中找到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 = /usr/local/bin/mysql_config

将最后一句句首井号去掉,并修改为:

mysql_config = /usr/local/mysql/bin/mysql_config

然后执行

$ python setup.py install

一般说来,此时安装可以完成,但仍有问题,下文会继续阐述。

使用pip安装时没有办法修改site.cfg文件,因此可以通过修改OS X的系统环境变量来解决找不到mysql_config的错误。

修改OS X环境变量:打开终端,在终端中使用vim打开“~/.bash_profile”,如果没有安装vim,那就显示隐藏文件用文本编辑器打开,具体操作这里就不复述了。在.bash_profile中添加以下内容:

PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes

其中 VERSIONER_PYTHON_PREFER_64_BIT和VERSIONER_PYTHON_PREFER_64_BIT根据自己安装的MySQL进行选择。

$ sudo ln -s /usr/local/mysql/bin/* /usr/bin

解决 Reason: image not found 错误

安装完MySQL-python包后,让我们import MySQLdb,此时出现一个错误,错误最后一行写着 Reason: image not found。

解决方法是在终端执行:

$ sudo ln -s /usr/local/mysql/lib/libmysqlclient..dylib /usr/lib/libmysqlclient..dylib
$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

错误:

clang: error:  clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

经网上查证:http://www.tuicool.com/articles/zI7Vzu貌似是mac os的Xcode从5.1起给编译器规定对于未知参数传入视为error我们需要使用ARCHFLAGS将该error降级为warning因此最后的安装命令应该如下:

sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future python setup.py build

参考:

http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html