MySQL在Yosemite上的Python 2.7.6问题

时间:2015-03-20 18:08:51

标签: python mysql osx-yosemite

我需要通过Python脚本连接到本地计算机上的MySQL数据库。这是我的设置:

  • 已安装(并且已用于开发网络应用程序数月)MySQL 5.6.22(从here下载.dmg)
  • 运行Yosemite附带的预装python(2.7)
  • 已下载:MySQL-python-1.2.4b4.tar.gz并通过在解压缩文件夹中运行python setup.py install来通过终端安装

这是我在安装结束时得到的输出:

Installed /Library/Python/2.7/site-packages/distribute-0.6.28-py2.7.egg
Processing dependencies for distribute==0.6.28`
Finished processing dependencies for distribute==0.6.28
Processing MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg
creating /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg
Extracting MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg to /Library/Python/2.7/site-packages
Adding MySQL-python 1.2.4b4 to easy-install.pth file

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

但是在尝试连接时,我的py脚本给了我这个错误:

ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-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.10-intel.egg/_mysql.so
Reason: image not found
('result', 256)

当我这样做时

$ python
>>> import MySQLdb

我得到了同样的错误。

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:0)

首先,您应确保拥有libmysqlclient.18.dylib。在我的Mac上,它的路径是:/opt/local/lib/mysql56/mysql/libmysqlclient.18.dylib。如果没有,可能需要安装mysql5-devel。

如果你有动态库,原因是_mysql.so的信息是错误的。您可以使用otool命令进行检查。

otool -L /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/_mysql.so

它将显示此命令告诉您的路径不是动态库的正确路径。您可以使用以下命令进行修复。

sudo install_name_tool -change libmysqlclient.18.dylib {this is the correct path for mysql dynamic library} /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/_mysql.so