« »

How to install MySQL-python 1.2.3c1 on Mac OS X

Tuesday, 26 May 2009

Introduction

Just a quick note on getting MySQL-python (aka import MySQLdb) 1.2.3c1 (the current latest version) to build and install on Mac OS X, because I hit something that I didn't see mentioned in a number of similar posts.

Here are some links that discuss getting MySQL-python to build on Mac OS X:

What follows are the steps (slightly different) that I needed to get MySQL-python to install.


How to install MySQL-python on Mac OS X

  1. My Setup

    Mac OS X 10.5/Intel
    Xcode 3.0
    ActivePython 2.6
    

    Though I am using ActivePython, the issues should be the same for a Python from python.org.

  2. Download and install MySQL 'pkg' format install for Mac OS X. For me this was the "Mac OS X 10.5 x86" package: mysql-5.1.34-osx10.5-x86.dmg The following might work:

    wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.34-osx10.5-x86.dmg/from/http://mirror.csclub.uwaterloo.ca/mysql/
    
  3. Download the latest MySQL-python package.

    cd /tmp
    wget http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3c1.tar.gz
    tar xzf MySQL-python-1.2.3c1.tar.gz
    cd MySQL-python-1.2.3c1
    
  4. Build it.

    # ensure mysql_config is on your PATH
    export PATH=$PATH:/usr/local/mysql/bin
    python setup.py build
    

    For me this failed as follows:

    gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -bundle -undefined dynamic_lookup build/temp.macosx-10.3-i386-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc -o build/lib.macosx-10.3-i386-2.6/_mysql.so
    ld: warning in build/temp.macosx-10.3-i386-2.6/_mysql.o, file is not of required architecture
    ld: warning in /usr/local/mysql/lib/libmysqlclient_r.dylib, file is not of required architecture
    ld: warning in /usr/local/mysql/lib/libmygcc.a, file is not of required architecture
    

    I didn't see this mentioned in others' post on this. I suspect they may not have hit this because they were building against the system Python (in /usr/bin/python, /System/Library/Frameworks/Python.framework/Versions/Current) which may have some tweaks to just handle this.

    In any case the problem here is that my Python install (ActivePython 2.6) is a universal build (including i386 and ppc). By default distutils (the library behind python setup.py build) tries to build binary Python extensions for all the same architectures. However, the MySQL you just installed is only for x86 so it borks.

    The fix is to use the ARCHFLAGS environment variable that distutils will pick up on to only build for your architecture:

    ARCHFLAGS=`arch` python setup.py build
    
  5. Install it.

    sudo python setup.py install
    

Tagged: install, python, activestate