I've been at this for some time and read many sites on the subject. suspect I have junk lying about causing this problem. But where?
This is the error when I import MySQLdb in python:
>>> import MySQLdb
/Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg/_mysql.pyc, but /Users/phoebebr/Downloads/MySQL-python-1.2.3c1 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.6-universal/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so: mach-o, but wrong architecture
I'm trying for 64 bit so checked here:
file $(which python)
/usr/bin/python: Mach-O universal binary with 3 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
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc
file $(which mysql)
/usr/local/mysql/bin/mysql: Mach-O 64-bit executable x86_64
Have set my default version of python to 2.6
python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Tried deleting build directory and python setup.py clean Renamed Python/2.5/site-packages so it could not try and pick that up.
Deleted everything and followed the instructions here: Django + MySQL on Mac OS 10.6.2 Snow Leopard installing using macports.
But basically still get the same error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/_mysql.so, 2): no suitable image found. Did find:
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/_mysql.so: mach-o, but wrong architecture
>>>
I have a fresh MacBook Air, and I managed to get MySQLdb working by doing the following: (Snow Leopard 10.6.6, preinstalled Python)
uname -a
Darwin Braindamage.local 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
Download the MySQL 32-bit dmg file from mysql pages, Install it.
Add the following lines to your ~/.profile (or ~/.bash_profile):
PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes
After saving, type the following in the terminal: source ~/.profile
Download the MySQL-python-1.2.3.tar.gz unzip, untar, cd to that directory
python2.5 setup.py build
sudo python2.5 setup.py install
exit that directory (or you'll get a warning)
python2.5
import MySQLdb
or
python
import MySQLdb
works the way it should!!
I just struggled with the same, despite the many answers, so I'll risk adding another:
python -c 'import platform; print platform.platform()'
. Does it end in "64 bit"?ls -l /usr/local/mysql
. It's a symlink: does it end in "x86_64"?If python says "64 bit", then you want mysql for "x86_64" (search for that at http://dev.mysql.com/downloads/mysql/). If python says "32 bit", then you probably want the "x86" mysql. If you have a match, but it still doesn't work, then read the other answers (about VERSIONER_PYTHON_PREFER_32_BIT etc.)
For me, the mismatch caused the "mach-o, but wrong architecture"
error. The next error was "Library not loaded: libmysqlclient.18.dylib... Reason: image not found"
.
To solve this one, I recommend adding a symlink (rather than set DYLD_LIBRARY_PATH, as explained in other answers):
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With