Setting up MySQL for Python (MySQLdb) on Mac OS X

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/.

    1. Download MySQL from mysql.com and install the DBMS as instructed in the documentation.
    2. 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.

  1. Download and unpack MySQL for Python. At the time of writing this is MySQL-python-1.2.1_p2.tar.gz .
  2. Open Terminal and change to the directory where MySQLdb was unpacked to.
  3. Get rid of any previous builds that might interfere, by deleting the “build” directory if it exists.
  4. Edit the setup.py file, and change:

    return popen(“mysql_config –%s” % what)
    to
    return popen(“/usr/local/mysql/bin/mysql_config –%s” % what)

  5. Cleanup any previous install attempts:

    python setup.py clean

  6. Build MySQLdb:

    python setup.py build

Viola! MySQL for Python is all setup, and ready for your Python-MySQL data access code. Enjoy!

Advertisements

4 Comments on “Setting up MySQL for Python (MySQLdb) on Mac OS X”

  1. joel says:

    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)
    >>>

  2. datasmid says:

    mysql-5.0.67-osx10.4-universal.tar.gz avoids wrong archs

  3. S says:

    This worked for me! thanks!

    I had to edit the posix version and change it to:

    f = popen(“%s –%s” % (“/usr/local/mysql/bin/mysql_config”, what))

    Again, thanks. This was very helpful.

  4. S says:

    This worked for me! thanks!

    I had to edit the posix version and change it to:

    f = popen(“%s –%s” % (“/usr/local/mysql/bin/mysql_config”, what))


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s