简体   繁体   中英

Error importing MySQLdb

I have been trying for awhile now to get MySQLdb working. I am currently getting this error:

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

Anyone know what I need to do in order to correct these errors?

It looks like MySQLdb has compiled itself for the wrong architecture. I'm running OSX Lion x86_64 and I had to do this:

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

and 64bit MySQL

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

You can verify the architecture of your current MySQLdb is 32bit:

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

And then the only way I could get MySQLdb to work was to download the source, and compile with

$ ARCHFLAGS='-arch x86_64' python setup.py install

which forces the setup.py to build a 64 bit library (_mysql.so) rather than 32 bit. (Hat-tip - I found this so question to be helpful.

HTH, Dan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM