MySQL Support from Python on OS X

By | February 19, 2008

It was more work than it should have been, but you can get access to a MySQL database from Python on OS X 10.5 by following the instructions at this blog post.

If you’re using Pydev in Eclipse, be sure /Library/Python/2.5/site-packages is in the System PYTHONPATH. Bring up the Preferences dialog, open Pydev in the tree, click on Interpreter – Python, click New Folder to the right of System PYTHONPATH, and then add the directory, if necessary.

Update 12/27/2009: With MySQL 5.1.41 and Snow Leopard (OS X 10.6), you no longer need to create the lib/mysql directory with symbolic links as described in the above article. Also, since Snow Leopard comes with Python 2.6, the path above for Eclipse should be /Library/Python/2.6/site-packages. I found Geert’s instructions to be quite helpful for Snow Leopard. There’s also a lot of useful info here.

One problem I ran into is that I initially built the MySQL Python connector with a 32-bit version of MySQL 5.1 installed. After upgrading, I forgot to delete the build directory to force the connector to completely rebuild. When I then tried to run “import MySQLdb” in the interpreter, I got the cryptic message “ImportError: dynamic module does not define init function (init_mysql)”. If I had read all of the comments in that last article before figuring this out, I might have gotten a hint from the comment mentioning that not even “setup.py clean” would delete the build directory if you left off the 64-bit architecture flags.

3 thoughts on “MySQL Support from Python on OS X

  1. Roger

    This is not particularly relevant, but I find it interesting.
    Per the SQLite folks, Mac OS X uses SQLite for Apple Mail and Safari. See:
    http://www.sqlite.org/famous.html
    And the new Filemaker, Inc. product (Bento) apparently relies on SQLite.

    Reply
  2. Robert Post author

    SQLite is a very nice embedded database. It’s also used by Google Gears. It seems to be becoming the cross platform embedded database of choice. I’ve used two others that are written in Java that are also very good – HSQLDB (formerly Hypersonic SQL) and Apache Derby (formerly Cloudscape). Derby is packaged with Sun’s Java JDK 6.

    Reply
  3. Pingback: WombatNation » MySQL, Python and Snow Leopard

Leave a Reply

Your email address will not be published. Required fields are marked *