Thursday, June 19, 2008

Installing MySQL for Python on Mac OSX Leopard

Yesterday, I have a sudden request from sysadmins group to make a web data presentation from their OVO data. I've already had some kind of template for the web apps using CodeIgniter based framework. The only "work" to do now is how to get, process and displaying the data from raw. It's gonna be supplied via text file. So, I came up with an idea to use Python to do collection, conversion and inserting into database :P Ya ya ya, I actually I just wanna try to learn Python and use it in a production environment :D So, it's gonna be my first Python application hehehehehe. My yukiyanagi (MB403LL/A MacBook) is already equipped with Python 2.5.1. Done with file checking, reading, parsing etc. It's now time to put all parsed data into database. What you need to do are:
  1. Download MySQL from dev.mysql.com/downloads and install it as instructed in the documentation.
  2. Download and extract the MySQL for Pythom from sourceforge.net. I'm using MySQL-python-1.2.2.tar.gz
  3. Open Terminal and go to the directory where MySQL-python-1.2.2.tar.gz was unpacked to.
  4. It's better to delete build/ directory, just in case your installation might be interfered with any previous builds
  5. edit file site.cfg, and edit the value of mysql_config to the correct PATH where your mysql_config reside. Mine is:
    # The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird # setup that requires it. mysql_config = /usr/local/mysql/bin/mysql_config
  6. After that, run the build and installation script.
    python setup.py build python setup.py install
During "python setup.py build", you might have some problem here like I did. It seems that there is an extra 'mysql' in the path that the shared object looks for the .dylib. You may get an error like this:
ImportError: dlopen(./_mysql.so, 2): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient_r.15.dylib Referenced from: ./_mysql.so Reason: image not found
The solution is by making a symlink from /usr/local/mysql/lib to /usr/local/mysql/lib/mysql
cd /usr/local/mysql/lib ln -s /usr/local/mysql/lib mysql
Another error which might be occured is gcc related like the picture below: MySQL for Python installation error Line 47 of /usr/local/mysql/include/mysql.h is:
#include <sys/types.h>
Line 93 (and 93) of /usr/include/sys/types.h is:
typedef unsigned short ushort; /* Sys V compatibility */ typedef unsigned int uint; /* Sys V compatibility */
All you need to do is comment those 2 lines (dont forget to make the backup first) and do the "python setup.py build". Voila! Everything will be running well.

Read this post on my new blog
http://nuy.jirolu.net/2008/06/19/installing-mysql-for-python-on-mac-osx-leopard/