Python mysqldb:库未加载:libmysqlclient.18.dylib


172

我刚刚在Mac OS 10.6上为python 2.7编译并安装了mysqldb。我创建了一个简单的测试文件,可以导入

import MySQLdb as mysql

首先,此命令带有红色下划线,并且信息告诉我“未解决的导入”。然后我尝试运行以下简单的python代码

import MySQLdb as mysql

def main():
    conn = mysql.connect( charset="utf8", use_unicode=True, host="localhost",user="root", passwd="",db="" )

if __name__ == '__main__'():
    main()

执行它时,我收到以下错误消息

Traceback (most recent call last):
  File "/path/to/project/Python/src/cvdv/TestMySQLdb.py", line 4, in <module>
    import MySQLdb as mysql
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/__init__.py", line 19, in <module>
    \namespace cvdv
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
  Reason: image not found

解决我的问题的方法可能是什么?

编辑:实际上我发现该库位于/ usr / local / mysql / lib中。所以我需要告诉我的pydev eclipse版本在哪里找到它。我在哪里设置?

Answers:


323

我通过创建到库的符号链接解决了这个问题。即

实际的库位于

/usr/local/mysql/lib

然后我在其中创建了一个符号链接

/usr/lib

使用命令:

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

这样我就具有以下映射:

ls -l libmysqlclient.18.dylib 
lrwxr-xr-x  1 root  wheel  44 16 Jul 14:01 libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib

就是这样 之后,一切正常。

编辑:

请注意,自MacOS El Capitan以来,系统完整性保护(SIP,也称为“无根”)将阻止您在中创建链接/usr/lib/。您可以按照以下说明禁用SIP ,但可以在其中创建链接/usr/local/lib/

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

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

我已经mysql55通过MacPorts 安装并解决了此错误:sudo ln -s mysql/libmysqlclient.18.dylib /opt/local/lib/mysql55/libmysqlclient.18.dylib
philfreo,2012年

小牛删除了旧的符号链接后,我不得不从一个略有不同的位置进行符号链接: sudo ln -s /usr/local/mysql-5.5.29-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Matt

2
如果您在El Capitan上运行,则必须禁用SIP:forums.developer.apple.com/thread/7935
约书亚·品特

全新安装El Capitan后,我发现您无需为此提示禁用SIP。
罗诺谢伊(Lonoshea)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.