导入MySQLdb时出错

时间:2012-01-08 21:34:48

标签: python python-import

我已经尝试了一段时间以使MySQLdb正常工作。我目前收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
  Referenced from: /Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
  Reason: image not found

任何人都知道我需要做些什么来纠正这些错误?

1 个答案:

答案 0 :(得分:0)

看起来MySQLdb已经为错误的架构编译了自己。我正在运行OSX Lion x86_64,我不得不这样做:

$ file $(which python) # so I can check my python is 64 bit

和64位MySQL

$ file $(which mysqld) # so I can check my MySQL is 64 bit too

您可以验证当前MySQLdb的体系结构是否为32位:

$ file /Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so

然后我可以让MySQLdb工作的唯一方法是下载源代码,并使用

进行编译
$ ARCHFLAGS='-arch x86_64' python setup.py install

强制setup.py构建64位库(_mysql.so)而不是32位。 (帽子提示 - 我发现this所以问题很有帮助。

HTH,Dan