Error importing MySQLdb in Python 2.7.2 on Mac OSx 10.8.1

走远了吗. 提交于 2020-01-17 04:26:05

问题


I recently upgraded to OSx Mountain Lion (10.8) and shortly after to 10.8.1. I was attempting to do an "import MySQLdb" and ran into the following errors:

>>> import MySQLdb
/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg/_mysql.pyc, but /Users/jlk/Software_Downloads/MySQL-python/MySQL-python-1.2.3 is being added to sys.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
  File "build/bdist.macosx-10.8-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.8-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/jlk/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg-tmp/_mysql.so, 2): no suitable image found.  Did find:
    /Users/jlk/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg-tmp/_mysql.so: mach-o, but wrong architecture

Not really sure what is wrong or how to fix it. I have googled around, but am unable to find any answers pertaining to osx 10.8.1 with this issue. It states at the end that its the "wrong architecture", but I know that what I have installed is the OSx version of the module. The only difference I see is that it says its for 10.8, but this is the most current version of the module.

For reference:

OS: OSx Mountain Lion 10.8.1 Pyhon version: 2.7.2

Any ideas?

Regards,

Jeff


回答1:


It's likely that the python interpreter you're using and the _mysql.so were built for different sets of architectures, e.g. one is i386 only and the other one 32/64 bit fat. You can check that by running file /path/to/your python interpreter and file /path/to_mysql.so and comparing the output.

Can you rebuild MySQLdb and make sure pip / easy_install uses the correct python interpreter? Also, if you haven't done so already, upgrade your Xcode version and its command line tools to avoid the usual spew of build errors.

edit: I could reproduce this on 10.7 with Apple's stock Python 2.7.1. The problem really is an architecture mismatch between the python interpreter, MySQLdb and the MySQL server. Python is a 32/64bit fat build, the MySQL version I have installed is 32bit only. MySQLdb looks at MySQL's architecture when building, so I got a 32bit only _mysql.so. When importing the module in python which probably runs in 64bit mode, the error occurs.

I think installing and running a 64bit version of MySQL and the rebuilding MySQL-Python should solve this, or you could force Python to run in 32bit mode.



来源:https://stackoverflow.com/questions/12327737/error-importing-mysqldb-in-python-2-7-2-on-mac-osx-10-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!