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

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



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

  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:
  3. Download the latest MySQL-python package.
    cd /tmp
    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 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/
    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 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 build
  5. Install it.
    sudo python install

Recent Posts

Webinar - Walking Dead Past Python EOL
Walking Dead Past Python EOL

With Red Hat dropping Python 2 support, more organizations will be stuck maintaining zombie legacy apps. Stop racing against EOL dates and letting bad practices infect your new projects. Get current and stay current with the latest open source language versions.

Read More
Scroll to Top