MySQL-Python on MacOSX 10.6 Snow Leopard

GCC was reporting a file not found error for stdarg.h early in the compile of MySQL-Python-1.2.3 on MacOSX 10.6 (Snow Leopard). I was staring right at the file, “/Developer/SDKs/MacOSX10.4u.sdk/usr/include/stdarg.h,” so it most certainly did exist. The problem seems to have been caused by the GCC compiler version and architecture I was using with my Python, Apache and MySQL installations.

$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
$ mysql --version
mysql Ver 14.14 Distrib 5.1.35, for apple-darwin9.5.0 (i386) using readline 5.1
$ /usr/sbin/httpd -v
Server version: Apache/2.2.17 (Unix)
$ python --version
Python 2.6.2

This was not obvious until I went to the trouble of downloading the MacOSX10.4u.sdk files and still had the same error—but I will spare you the tale of the hours I lost banging my head.

For reasons that don’t matter here, I had previously manually compiled both Apache and MySQL to use 32-bit architecture. As I understand from all the help I looked for online (this site where my issue with stdarg.h was part of the discussion was particularly helpful), all the various libraries, Python, and the GCC compiler need to be using the same architecture. I found two solutions:

MySQL-python-1.2.3 $ sudo CC=/usr/bin/gcc-4.0 python setup.py clean
MySQL-python-1.2.3 $ sudo CC=/usr/bin/gcc-4.0 python setup.py build
MySQL-python-1.2.3 $ sudo CC=/usr/bin/gcc-4.0 python setup.py install
MySQL-python-1.2.3 $ sudo ARCHFLAGS='-arch x86' python setup.py clean
MySQL-python-1.2.3 $ sudo ARCHFLAGS='-arch x86' python setup.py build
MySQL-python-1.2.3 $ sudo ARCHFLAGS='-arch x86' python setup.py install

Everything compiled with either one of these. The latter seems to me to be the better solution. After running it, I could load the library in Python with no errors:

>>> import MySQLdb

I was then able to get Django to talk with MySQL and set up a database. Yay!

Notes: For the compile to work you will also need the Python setuptools module. I installed it using easy_install, which I had evidently installed without installing the full setuptools package.

Django, PYTHONPATH, and iPython

After trying Drupal and finding it needlessly twisted, I have decided to experiment with Django. I’m working my way through Python Web Development with Django®, by Jeff Forcier, Paul Bissex and Wesley Chun, published by Addison-Wesley Professional in 2008. Thing have gone smoothly, so far, though I have not yet set up Apache to work with Python and Django either locally or on my server. My previous experience has been limited using Python for cgi scripting.

I followed the Django installation instructions at http://docs.djangoproject.com/ to install Django 1.2.5 (current version is 1.3), generally without a hitch. Following instructions in Appendix B of the book, I ran the command python setup.py install from the Django directory. Also at the suggestion of the book, I installed iPython. It has some handy features, but also seems to have far more features than I need. I’ve also had a problem with its recognizing my Django directory as part of its search path. The regular Python editor does not have this problem.

To inspect the search path, start python:

$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path

This should print out a mutable list of paths. The problem is, the list is different if you invoke ipython as the editor. In fact, it’s missing the path that contains the Django library. As a result typing

>>> import django

in iPython will return an error message. It works just fine in the regular old Python editor.

The remedy? For scripts that are run from the Terminal, you need to create or modify the PYTHONPATH environmental variable, which stores the search path returned by sys.path in Python. Even if you haven’t set this variable, the standard Python editor apparently gets all the necessary information from other path variables in Mac OSX, while iPython doesn’t. It seems to be missing one specific path. To add it in, edit your ~/.bash_profile file to contain the following line:

export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:$PYTHONPATH

You can add other paths to this later on, as you develop your own modules. Other techniques for adding to the search path involve .pth files or, for apps launched from the finder, creating or modifying ~/.MacOSX/environment.plist.