Python MySQL wrong architecture error

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.

UPDATE

Deleted everything and followed the instructions here: http://stackoverflow.com/questions/1904039/django-mysql-on-mac-os-10-6-2-snow-leopardinstalling 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
>>> 
share improve this question
 
3 
Your library (_mysql.so in /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-‌​packages/_mysql.so) is the wrong architecture (very likely 32-bit). You're almost there. :) –   vladr  Jun 30 '10 at 17:52 
 
Sadly, trying to rebuild from source code caused an error The-Black-Book-2:MySQL-python-1.2.3 phoebebr$ ARCHFLAGS='-arch x86_64' python setup.py build sh: mysql_config: command not found Uninstalled all the macports install and doing a reinstall from scratch again in the hope that all the versions match up this time. –   PhoebeB  Jul 1 '10 at 21:22
 
OK uninstalled all macports installs, removed everything on my hard disk related to mysql. Installed mysql from package on mysql site, downloaded python-mysql and built it using the same instructions I used originally, only this time it worked. Have no idea what was different this time, but it is working... Thanks to jdinuncio and belvoir - you were both right but had to pick one so chose the first person who responded! Hope this is ok. –   PhoebeB  Jul 3 '10 at 19:41
 
I have been struggling with MySQLDB since last evening. My config: Mac OS X 10.5.8; Python 2.5; had downloaded and installed 64 bit MySQL with the DMG; installed MySQLDB using their readme; I got the error that is mentioned in the question. Tried different approaches at other websites. Solution in last post by Pekka Toiminen Feb 22 at 17:08 worked for me. Thank you! –   user635989  Feb 26 '11 at 23:11

10 Answers

up vote 1 down vote accepted

When the interpreter says is:

You have installed MySQL_python-1.2.3c1 in /Library/Python/2.6/site-packages but you are adding to sys.path another version in /users/phoebebr/Downloads. When I try to import MySQLdb from the second directory, I've found that _mysql.so is from another architecture.

SO, it seems that you ended with the wrong version of MySQLdb. Delete /Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp and /Users/phoebebr/Downloads/MySQL-python-1.2.3c1. Test again to see if the version in /library works. If not, donwload the binary for MacOS. In last instance, download the source of MySQL-python and compile it.

share improve this answer
 
 
Thanks for your translation and suggestion. I have deleted both the items in /Users/phoebebr but every time I go into python and try the import command it recreates the /Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.eg‌​g-tmp file again! Have looked at sys.path and there is no reference to this path there. What other path could be set that is confusing it? –   PhoebeB  Jun 17 '10 at 23:59
 
Oh, that's estrange. I'm not familiar with python in MacOS, but the original cause still remains. It seems that the package in /Library... is the one with the wrong architecture. Delete it, download a new version and reinstall it. –   jdinuncio  Jun 18 '10 at 12:24
 
This is driving me crazy. Deleted everything and reinstalled using sudo easy_install-2.6 -m mysql-python and it created the /Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.eg‌​g-tmp again. Deleted everything again and tried building from a download, using instructions cd34.com/blog/tag/mysql-python and it still recreates the (&&*&^ /Users/phoebebr/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.eg‌​g-tmp. Am packing it in after another whole evening at this. Aghhhh. –   PhoebeB  Jun 21 '10 at 20:10

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!!

share improve this answer
 
 
This is clear, thank you. I think the key is installing the 32-bit version of MySQL so things play nice with Python. I have been trying for hours to just get this to work and after ditching the 64-bit things went much more smoothly. –   themaestro  Mar 13 '11 at 19:31
 
Thank you so much for this guide. The .profile pieces were apparently what I was missing. –   Chris Ridenour Jul 12 '11 at 15:54
 
Confirm that this also works on OSX 10.7 Lion! –   AndersG  Jan 19 '12 at 13:12
 
Works well on OX 10.8.2, the interface version is MySQL-python-1.2.4. Great thanks. –   jowett  Mar 10 '13 at 3:24
 
Did not work for me - still get /Users/kellyfj/.python-eggs/MySQL_python-1.2.5-py2.7-macosx-10.7-x86_64.egg-tmp‌​/_mysql.so: mach-o, but wrong architecture –   kellyfj  Feb 15 '14 at 18:31 

I just struggled with the same, despite the many answers, so I'll risk adding another:

  • Run python -c 'import platform; print platform.platform()'. Does it end in "64 bit"?
  • Do 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/
share improve this answer
 

It's caused by that your Python is 32bits, but somehow, the installed MySQL library is 64bits. To solve the problem, here you can install it manually with following commands:

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar xvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
ARCHFLAGS="-arch i386" python setup.py install

With ARCHFLAGS="-arch i386", it should be compiled as i386 architecture.

share improve this answer
 

This is a shot in the dark - not familiar with MACOSX - but I saw a similar problem under Linux and could only resolve it by:

  1. Uninstalling all MySQLdb components via the package manager
  2. Doing a low level search to locate any remaining directories and files relating to MySQLdb - if you manually installed/built MySQLdb you will probably find some references to it somewhere but most likely in the site-packages directory - I did and simply deleteing them is the recommended approach based upon the research I did

Next I tried to import MySQLdb and made sure I got a simple error that the package did not exisit - at least I knew MySQLdb was 100% removed

Then I a clean install via the package manager if that option exists as it will be 100% compatible with your platform and libs. Compiling etc. is great but you have to do lots of leg work to make sure that you have the right MySQL client libs etc. to link to (based upon my painful experience)

Good luck.

Worst case ... you could use the alternative PyMySQL pure python option (http://pypi.python.org/pypi/PyMySQL/0.2) but I have to confess most people recommend MySQLdb

share improve this answer
 
 
Yes the start again (again, again) option seems to be working. Deleted absolutely everything to do with python and mysql and reinstalling. Used mysql package from mysql site and built python-mysql (as instructed here cd34.com/blog/tag/mysql-python) and so far so good. As this is exactly what I did the first time it is more than a little frustrating! –   PhoebeB  Jul 2 '10 at 16:46
 
Did you see these ... brainfault.com/2008/04/18/… and mangoorange.com/2008/08/01/… Good luck ... might be quicker to learn to program in C! –   belvoir  Jul 3 '10 at 10:52
 
All working now that I've started from scratch again. Have added comment above. Now sure which answer to select as you are both right! –   PhoebeB  Jul 3 '10 at 19:37

It appears that this issue is related to the version of mysql that you have on the box. When compiling the python mysql component it uses mysql_config which usually lives in /usr/local/mysql/bin to determine what flags to pass to CC when compiling the mysql component. If you have a 32 bit version of mysql running then you will get a 32 version of this component even if you have the 64 bit version of python. As an example I had the following version of mysql...

/usr/local/mysql-5.5.16-osx10.6-x86 and I should have been running... /usr/local/mysql-5.5.16-osx10.6-x86_64 

Changing over to the 64bit version of mysql on my machine and then running... sudo pip install mysql-python 

fixed the problem for me. You can run the following command to verify how it is going to build the mysql component...

/usr/local/mysql/bin/mysql_config --cflags

You should see something like this...

-I/usr/local/mysql/include -Os -g -fno-common -fno-strict-aliasing -arch x86_64

share improve this answer
 

I got another possible solution to add.

I solved the problem by adding the following line to .profile (.bash_profile is ok if it's a development machine):

export VERSIONER_PYTHON_PREFER_64_BIT=yes
export VERSIONER_PYTHON_PREFER_32_BIT=no

(2nd line might not be necessary, though. But after hours of fiddling, trying, recompiling, I just couldn't be bothered trying this out anymore).

share improve this answer
 

This problem was solved for me by selecting the correct python version. If you installed with macports:

sudo port select python python26
share improve this answer
 

I met the same problem. 

  • My situation: Mac OSX 10.6, python is 64 bit, mysql is 32 bit, and _mysql.so is 32 bit.

  • Solution: uninstall mysql (http://steveno.wordpress.com/2009/03/26/uninstall-mysql-on-mac-os-x/), and install 64 bit mysql. Then make sure mysql_config is pointed to the one you just installed. 

IMPORTANT:
remove all the _mysql.so in the MySQL-python-1.2.3 directory (this is what you download) or just remove the whole directory and generate it again from the tar file. Then build and install again. Then _mysql.so will be 64 bit. Now you can import it. If you see dyld: Library not loaded, then export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

share improve this answer
 

I had the same problem with MySQLdb in Mac OS X and Ubuntu, so I switched to the official MysQL Python Connector: (available on PyPI):

sudo pip install mysql-connector-python

or by download from: http://dev.mysql.com/downloads/connector/python/

Documentation: http://dev.mysql.com/doc/refman/5.5/en/connector-python.html

It's easy to use and also compatible with PEP 249 (Python DB API version 2.0).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值