Setting up MySQL for Python (MySQLdb) on Mac OS XPosted: 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”
Either uninstall MySQL for Python if a setup program was used to install it, otherwise manually delete them.
- 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)
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
Viola! MySQL for Python is all setup, and ready for your Python-MySQL data access code. Enjoy!