Friday, July 09, 2010

Installing MySQL-python (MySQLdb) in a virtualenv on OSX 10.6

There were two problems with the MySQL-Python package that I had when installing on OS X 10.6
1) First, an error saying "EnvironmentError: mysql_config not found" when setup.py is run (through pip or easy_install)
2) Second, an error saying "ImportError: dynamic module does not define init function(init_mysql) when importing MySQLdb" when 'import MySQLdb" is issued

Causes:
1) This is caused by the build script not being able to find a MySQL program called mysql_config. This program is used to determine metadata about the mysql install. For example, the command "mysql_config --cflags" reports the flags used to build mysql.
2) This is caused by a mismatch between the architectures that MySQL was built with and the architecture that MySQL-python installer is trying to build and install MySQL-python as. To find what arch MySQL was built as, run
"mysql_config --cflags" (this was i386 in my case). OSX tries to build MySQL-python as x86_64. Therefore, there is a arch mismatch.

Solution:
1) Locate the mysql_config binary location and add the path to the MySQL-python/site.cfg file as a config directive: "mysql_config = /opt/local/bin/mysql_config5"
2) Use the arch x86_64 (64 bit) version of MySQL compiled through MacPorts, not a i386 (32 bit) version downloaded off the internet and installed as a dmg.

Summary:
Assuming the MacPorts version of MySQL is installed, both issues can be solved by adding the mysql_config = /opt/local/bin/mysql_config5 config directive to the MySQL-python/site.cfg file.

# SCRIPTED SOLUTION: Create a script called setup.sh and add the following lines
virtualenv --distribute --no-site-packages ve
source ./ve/bin/activate
pip install -E ./ve -r requirements.1.pip # other deps
export ARCHFLAGS="-arch x86_64"
pip install -E ./ve MySQL-python
echo "mysql_config = /opt/local/bin/mysql_config5" >> ./ve/build/MySQL-python/site.cfg
pip install -E ./ve MySQL-python


Resources:
http://groups.google.com/group/python-virtualenv/msg/cf4f3117faea476b?pli=1
http://birdhouse.org/blog/2009/02/21/python-mysql-connections-on-mac-os/
http://stackoverflow.com/questions/2111283/how-to-build-64-bit-python-on-os-x-10-6-only-64-bit-no-universal-nonsense

3 comments:

Anonymous said...

Very helpful. Thank you.

dubnsnow said...

I face the same error "mysql_config not found"
but according to your suggested solution there is no site.cfg in my Mac OS x.
Any help appreciated

Joe Jasinski said...

It's been awhile since I wrote this post, but if I recall correctly, the site.cfg is located in an "build" directory created in your virtualenv by pip. Pip will automatically remove the build directory if the build was successful, but usually leaves it there on failure. Is that were you were looking?
Joe