无法在 Mac OS Lion 上将 mysqlclient.18.dylib 加载到 python 中 [英] Can't load mysqlclient.18.dylib into python on Mac OS Lion

查看:45
本文介绍了无法在 Mac OS Lion 上将 mysqlclient.18.dylib 加载到 python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get MySQL working with the system Python 2.7 in Mac OS X Lion.

我知道有很多与此非常相似的问题,实际上我的症状与 mysql-python 安装问题(在 mac os x lion 上).但是,这个答案似乎不适用于我的情况.

I know there are a lot of questions very similar to this out there, and in fact my symptom is identical to mysql-python installation problems (on mac os x lion). However, that answer doesn't seem to apply to my case.

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

My Mac is a current MacBook Pro running the 64-bit kernel:

% 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 是一个胖二进制:

The stock python on Lion is a fat binary:

% 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 install of MySQL onMac OS X 删除旧的 MySQL 安装.

I had used Migration Assistant so my system had an old installation of MySQL on it. I used the instructions at How to remove installation of MySQL on Mac OS X to remove the old MySQL installation.

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

MySQL 64-bit 5.5.19 was installed from the .dmg on the MySQL site. As usual, this placed the client library in /usr/local/mysql/lib. The client library appears to be exactly what we want:

% 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 位图像,尽管结果与或没有):

However, that shared library can't be loaded into python (I'm using the arch command to make absolutely sure I'm running the 64-bit image although the result is the same with or without that):

% 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 方式,因为我试图弄清楚问题是出在 mysql-python 中还是出在 MySQL 客户端库中.从上面看,似乎是客户端库.但是,我不明白为什么加载程序认为客户端库是错误的架构.

I can reproduce this problem many other ways, notably by using the standard import MySQLdb; I'm showing the ctypes way of reproducing the bug specifically because I was trying to figure out whether the problem was in mysql-python or in the MySQL client library. From the above, it seems to be the client library. However, I'm baffled as to why the loader thinks the client library is the wrong architecture.

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

In trying to see whether it's some dependent library that's the problem, I checked the dependencies in 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 是预期的胖二进制文件:

And of course /usr/lib/libSystemB.dylib is a fat binary as expected:

% 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 位的...任何关于加载器拒绝导入库的猜测都将非常受欢迎.

So... Python is 64-bit, the mysqlclient library is 64-bit, and the dependent libraries are all 64-bit... Any guesses as to why the loader is refusing the import the library would be very welcome.

只是为了检查所有东西都是 64 位的:

Just for the belt-and-suspenders checking that everything's 64-bit-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,为此在 这个话题.

At this point, since I need to actually get some work done, I'm going to try switching to pymysql. This is mostly for use with Django, for which there's a convenient way to swap pymysql in documented at the bottom of this thread.

附言我了解 macports,但无法在此系统上使用它.

P.S. I understand macports and can't use it on this system.

推荐答案

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

Chances are you have set the default execution mode of the Apple-supplied Python to 32-bits, either by using defaults or by setting the VERSIONER_PYTHON_PREFER_32_BIT environment variable; see man python for details. In OS X 10.6 and 10.7 /usr/bin/python is an Apple-supplied wrapper program that determines which Python to run and in which mode. Using arch to execute /usr/bin/python will not influence the interpreter. For example:

$ 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

这篇关于无法在 Mac OS Lion 上将 mysqlclient.18.dylib 加载到 python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆