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

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

叨叨

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

安装过程主要是mysql_config not found错误,这个错误一般卡住不少初学者,以及安装后出现的 image not found 错误mysql

版本:Python 2.7.3sql

MySQL-python包中,所以不管下载仍是在pip中search,都应该是搜寻MySQL-python。vim

流程:

下载MySQLdbbash

MySQL-python-1.2.4b4.tar,下载后解压,而后在终端Terminal中执行如下命令:编辑器

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

使用PIP安装MySQLdbui

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

不管是在线安装仍是下载安装,此时你可能会遇到第一个错误提示:this

EnvironmentError: mysql_config not found

解决mysql_config not found错误spa

所以下载安装时的解决办法为:在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.18.dylib /usr/lib/libmysqlclient.18.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

相关文章
相关标签/搜索