Setting up MySQL for Python (MySQLdb) on Mac OS X
November 16, 2007
MySQL is an excellent, very popular and open source database management system. A binary installation is available for Mac OS, and the process of installing MySQL on Mac OS is also very well documented. So just head over to MySQL.com, where you will find the binary installation, together with extensive installation, and usage documentation.
To connect to MySQL from Python use MySQL for Python (MySQLdb). Get MySQLdb from http://sourceforge.net/projects/mysql-python/.
- Download MySQL from mysql.com and install the DBMS as instructed in the documentation.
- Make sure “Library/Python/2.3” does not have:
- Directory “MySQL”
- File “_mysql.pyd”
- File “_mysql_exceptions.py”
- File “_mysql_exceptions.pyc”
- Download and unpack MySQL for Python. At the time of writing this is MySQL-python-1.2.1_p2.tar.gz .
- Open Terminal and change to the directory where MySQLdb was unpacked to.
- Get rid of any previous builds that might interfere, by deleting the “build” directory if it exists.
- Edit the setup.py file, and change:
return popen(“mysql_config –%s” % what)
to
return popen(“/usr/local/mysql/bin/mysql_config –%s” % what) - Cleanup any previous install attempts:
python setup.py clean
- Build MySQLdb:
python setup.py build
Either uninstall MySQL for Python if a setup program was used to install it, otherwise manually delete them.
Viola! MySQL for Python is all setup, and ready for your Python-MySQL data access code. Enjoy!






June 11, 2008 at 2:31 am
Unfortunatly I am still getting this error …
>>> import MySQLdb
Traceback (most recent call last):
File “”, line 1, in
File “build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py”, line 19, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)
>>>
January 15, 2009 at 1:48 pm
mysql-5.0.67-osx10.4-universal.tar.gz avoids wrong archs