Mac系统Python安装MySQLdb的巨坑(已踩)

来源:互联网 发布:unity3d自学 编辑:程序博客网 时间:2024/05/04 08:12

下载MySQLdb

在SourceForge可以下载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安装MySQLdb

在终端中执行:
 
new-host-3:~ iFantastic$ pip install MySQL-python

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

建议使用pip安装,因为我从sourceforge下载的版本居然不如pip中的新。如果想要升级mysql-python版本,可以使用以下命令:
 
$ easy_install MySQL-python --upgrade

  [注意]如果你没有按照下文解决安装中的错误提示,那么升级也会因为同样的错误原因而失败。

解决mysql_config not found错误

在默认方式安装Mac版MySql时,会在/usr/local/mysql/bin目录下生成mysql_config文件。
因此下载安装时的解决办法为:在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的错误。

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

其中VERSIONER_PYTHON_PREFER_64_BITVERSIONER_PYTHON_PREFER_64_BIT根据自己安装的MySQL进行选择。
另外再提供一个pip安装时找不到mysql_config的解决方法(我没有验证),在终端中输入以下命令:
  
$ sudo ln -s /usr/local/mysql/bin/* /usr/bin

好了,到这里,MySQL-python包应该基本顺利安装。
  

解决 Reason: image not found 错误

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: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

再用pip下载一遍,此时如果还有报错,可加上–user。
例如:

pip install MySQL-python --user
1 0