无法在Mac OS Lion上将mysqlclient.18.dylib加载到python中

时间:2011-12-30 08:28:54

标签: python mysql osx-lion

我想让MySQL在Mac OS X Lion中使用Python 2.7系统。

我知道有很多问题与此非常相似,实际上我的症状与mysql-python installation problems (on mac os x lion)相同。但是,这个答案似乎并不适用于我的案例。

我的Mac是目前运行64位内核的MacBook Pro:

% uname -a 
Darwin Leos-MacBook-Pro.local 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug  9 20:54:00 PDT 2011; root:xnu-1699.24.8~1/RELEASE_X86_64 x86_64

Lion上的股票python是一个胖二进制文件:

% file /usr/bin/python
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):    Mach-O executable i386

我曾使用过迁移助手,因此我的系统上安装了旧的MySQL。我使用How to remove installation of MySQL on Mac OS X中的说明删除旧的MySQL安装。

MySQL 64位5.5.19是从MySQL站点上的.dmg安装的。像往常一样,这将客户端库放在/usr/local/mysql/lib中。客户端库似乎正是我们想要的:

% file /usr/local/mysql/lib/libmysqlclient.18.dylib
/usr/local/mysql/lib/libmysqlclient.18.dylib: Mach-O 64-bit dynamically linked shared library x86_64

然而,该共享库无法加载到python中(我正在使用arch命令来确保我正在运行64位图像,尽管结果是相同的,有或没有):

% arch -x86_64 python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary("/usr/local/mysql/lib/libmysqlclient.18.dylib")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/mysql/lib/libmysqlclient.18.dylib, 6): no suitable image found.      Did find:
    /usr/local/mysql/lib//libmysqlclient.18.dylib: mach-o, but wrong architecture
    /usr/local/mysql/lib/libmysqlclient.18.dylib: mach-o, but wrong architecture

我可以通过许多其他方式重现此问题,特别是使用标准import MySQLdb;我正在展示ctypes特别重现bug的方法,因为我试图弄清楚问题是在mysql-python还是在MySQL客户端库中。从上面看,它似乎是客户端库。但是,我很困惑为什么加载器认为客户端库是错误的架构。

在试图查看某个依赖库是否存在问题时,我检查了libmysqlclient.18.dylib中的依赖项:

% otool -L /usr/local/mysql/lib/libmysqlclient.18.dylib
/usr/local/mysql/lib/libmysqlclient.18.dylib:
libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)

当然/usr/lib/libSystemB.dylib是预期的胖二进制文件:

% file /usr/lib/libSystem.B.dylib 
/usr/lib/libSystem.B.dylib: Mach-O universal binary with 2 architectures
/usr/lib/libSystem.B.dylib (for architecture x86_64):   Mach-O 64-bit dynamically linked shared library x86_64
/usr/lib/libSystem.B.dylib (for architecture i386): Mach-O dynamically linked shared library i386

所以... Python是64位,mysqlclient库是64位,依赖库都是64位...任何猜测为什么加载器拒绝导入库非常受欢迎

只是因为皮带和吊带检查一切都是64位y:

% python -c 'import platform; print platform.platform()'
Darwin-11.2.0-x86_64-i386-64bit
% ls -l /usr/local/mysql
lrwxr-xr-x  1 root  wheel  27 Dec 10 16:52 /usr/local/mysql@ -> mysql-5.5.19-osx10.6-x86_64

此时,由于我需要完成一些工作,我将尝试切换到pymysql。这主要用于Django,在this thread的底部有一个交换pymysql的便捷方法。

P.S。我理解macports并且不能在这个系统上使用它。

1 个答案:

答案 0 :(得分:0)

有可能通过使用defaults或设置VERSIONER_PYTHON_PREFER_32_BIT环境变量,将Apple提供的Python的默认执行模式设置为32位;有关详细信息,请参阅man python。在OS X 10.6和10.7中/usr/bin/python是Apple提供的包装器程序,用于确定要运行的Python以及在哪种模式下运行。使用arch执行/usr/bin/python不会影响解释器。例如:

$ unset VERSIONER_PYTHON_PREFER_32_BIT 
$ arch -x86_64 /usr/bin/python -c "import sys;print(sys.maxsize)"
9223372036854775807
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes
$ arch -x86_64 /usr/bin/python -c "import sys;print(sys.maxsize)"
2147483647
#
# But avoiding the wrapper program ....
#
$ arch -x86_64 /usr/bin/python2.7 -c "import sys;print(sys.maxsize)"
9223372036854775807
$ arch -i386 /usr/bin/python2.7 -c "import sys;print(sys.maxsize)"
2147483647