mac os x下python安装MySQLdb模块

以前说了max os x下安装mysql,我们需要使用python访问mysql数据库,需要安装MySQLdb模块,方法如下:
在下面的网址下载mysqldb模块:
http://sourceforge.net/projects/mysql-python/
在mac os x直接双击解压,命令行进入解压后的目录, 执行python setup.py build
如果有
sh: mysql_config: command not found
提示,我们需要编辑下mysql的路径,使用vim打开setup_posix.py
找到:
mysql_config.path = "mysql_config"
改为:
mysql_config.path = "/usr/local/mysql/bin/mysql_config"
然后执行:
sudo python setup.py install
安装成功后,在命令行输入python进入python环境,输入import MySQLdb,我的环境中报下面的错误:
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in </module><module>
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
  Reason: image not found
解决方法,我们建立一个软链就可以了
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
这样我们就在mac os x的python环境下安装好了MySQLdb模块
原文地址:http://www.chenruixuan.com/archives/408.html

你可能感兴趣的:(mac os x下python安装MySQLdb模块)