Mac下安装MySQL-python

使用easy_install安装

sudo easy_install MySQL-python

报错信息中有这两行,没有mysql_config命令

sh: mysql_config: command not found
EnvironmentError: mysql_config not found

先找下这个命令在哪里,查到是mysql/bin下命令。难道mysql没有添加到我的环境变量path中。
检查下path,可能是没有配置这个命令

$ echo $PATH
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/bin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc:
/Users/chopin/apache-maven-3.3.9/bin:

确实没有配置,知道了这个问题解决起来就好办了。

解决方法

添加一个软链接,将命令放到环境变量/usr/local/bin下

ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

然后再执行安装命令

$sudo easy_install MySQL-python
16 warnings generated.
zip_safe flag not set; analyzing archive contents...
Adding MySQL-python 1.2.5 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.10-intel.egg
Processing dependencies for MySQL-python
Finished processing dependencies for MySQL-python

看到安装提示有16个警告,安装的版本是1.2.5

检测安装是否成功

使用help()方法来检测模块是否安装。

$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
//输入模块名
help> MySQLdb
problem in MySQLdb - : dlopen(/Users/chopin/.python-eggs/MySQL_python-1.2.5-py2.7-macosx-10.10-intel.egg-tmp/_mysql.so, 2): 
Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/chopin/.python-eggs/MySQL_python-1.2.5-py2.7-macosx-10.10-intel.egg-tmp/_mysql.so  
  Reason: image not found

又发现了错误,Library not loaded: libmysqlclient.18.dylib
找不到libmysqlclient.18.dylib这个库文件。
实际上这个文件在/usr/local/mysql/lib下的,所以又是文件路径引用错误。
再次做个软链接

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

再次在help下输入模块名MySQLdb,会输出说明文档,帮组熟悉模块内容。

help> MySQLdb
...
PACKAGE CONTENTS
    connections
    constants (package)
    converters
    cursors
    release
    times
...
//退出文档按q
//退出help输入quit
help> quit
//退出python输入
>>>exit()

好了,大功告成,可以尽情的开发了。

你可能感兴趣的:(Mac下安装MySQL-python)