Python: MySQLdb和"未加载库: libmysqlclient.16.dylib
设置环境...
我正在尝试在一台全新的 Mac OS X 10.6 上安装环境,以便开发 Python 和 Django,但我不记得在 10.5 上遇到过这个问题。
我从 mysql-5.5.8-osx10.6-x86_64.dmg
安装了 MySQL,然后我运行了:
$ sudo pip install MySQL-python
看起来一切都很顺利(下面是输出结果)
Downloading/unpacking MySQL-python
Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
Running setup.py egg_info for package MySQL-python
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
Installing collected packages: MySQL-python
Running setup.py install for MySQL-python
building '_mysql' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
In file included from _mysql.c:36:
/usr/local/mysql/include/my_config.h:325:1: warning: "SIZEOF_SIZE_T" redefined
In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:9,
from pymemcompat.h:10,
from _mysql.c:29:
/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pymacconfig.h:33:1: warning: this is the location of the previous definition
In file included from _mysql.c:36:
/usr/local/mysql/include/my_config.h:419:1: warning: "HAVE_WCSCOLL" redefined
In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:8,
from pymemcompat.h:10,
from _mysql.c:29:
/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyconfig.h:803:1: warning: this is the location of the previous definition
gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup build/temp.macosx-10.6-universal-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lpthread -o build/lib.macosx-10.6-universal-2.6/_mysql.so -arch x86_64
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
Successfully installed MySQL-python
Cleaning up...
之后我尝试了:
$ python -c "import MySQLdb"
结果却出现了问题,显示:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Python/2.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.6/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Library/Python/2.6/site-packages/_mysql.so
Reason: image not found
所以我想问...
我哪里做错了?/我还需要做什么?
在网上搜索这个问题(包括这里的搜索)发现很多人遇到这个错误信息,主要是和 Ruby 有关,而和 Python 相关的却不多。
7 个回答
42
在使用easy_install之后,我创建了一个软链接,这样就解决了问题。
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
97
只需要在运行完 pip install
或 easy_install
之后,设置一下 DYLD_LIBRARY_PATH
就可以了:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
这样做应该没问题,前提是你的 MySQL 安装在 /usr/local/mysql
这个路径下。
56
_mysql.so
是指 libmysqlclient.16.dylib
。也就是说,_mysql.so
是一个共享库,它充当了 Python 和 MySQL 客户端库之间的桥梁,而这个库的动态链接库由于某种原因无法加载。
你需要回答以下问题:
- 你的系统上有没有
libmysqlclient.16.dylib
这个文件?如果没有,你需要安装 MySQL 客户端软件。 - 如果有,这个库所在的文件夹是否在你的
DYLD_LIBRARY_PATH
设置中?如果没有,试着把它加进去。 - 如果有,你还需要确保
libmysqlclient.16.dylib
这个文件没有损坏。我在/opt/local/lib/mysql5/mysql/libmysqlclient.16.dylib
安装的版本,来自 MacPorts,MD5 校验码是c79ee91af08057dfc269ee212915801a
,文件大小是 1,462,376 字节。你的文件是什么样的?