views:

788

answers:

3

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-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
>>> 
+1  A: 

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.

jdinuncio
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.egg-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?
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
A: 

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

belvoir
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 http://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!
Did you see these ... http://www.brainfault.com/2008/04/18/install-python-mysql-mysqldb-on-mac-osx/ and http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/Good luck ... might be quicker to learn to program in C!
belvoir
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!
A: 

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).

Cheers Markus

Markus