Python


1/10/2010: 11:43 pm: RobertDjango, Python

Django includes a Bash shell completion script, which can save a lot of time. The tutorial recommends installing it, but doesn’t explain how. Fortunately, its the same as for any other Bash shell completion script. Here’s how I did it for Django.

  • Add an environment variable for the location where you installed Django. For example, add the following line to ~/.bash_profile:
    export DJANGO_HOME=/Users/rstewart/code/Django-1.1.1
  • Add a line after this one in ~/.bash_profile to read the script, e.g.,
    source $DJANGO_HOME/extras/django_bash_completion
  • After saving .bash_profile, open a new terminal or run source ~/.bash_profile

Now, you can press tab to complete commands. Below, I pressed tab after entering “manage.py”.

$ python manage.py
compilemessages   diffsettings      loaddata          runserver         sqlclear          sqlreset          syncdb
createcachetable  dumpdata          makemessages      shell             sqlcustom         sqlsequencereset  test
createsuperuser   flush             reset             sql               sqlflush          startapp          validate
dbshell           inspectdb         runfcgi           sqlall            sqlindexes        startproject
12/29/2009: 5:17 pm: RobertMac, MySQL, Python

I just updated an old post on using Python with MySQL on OS X. The good news is that you don’t need to create an extra directory of symbolic links anymore. The not really bad news is that you have to be careful about 32-bit versus 64-bit.

Ideally, you’ll first upgrade to a 64-bit version of MySQL 5.1 or later, unless you already have. Then, follow Geert’s simple instructions for building the MySQL Python connector.

7/22/2008: 8:37 pm: RobertHurricane Katrina, Python

While analyzing a bunch of US Census data at work to build out an even more comprehensive geographical database for Voxify’s speech apps, I ran into an unexpected error. I wrote a Python script to parse a CSV file that contains population data from 2000 and estimates for 2001-2007. The code extracted the data I was interested in, cleaned it up to remove extraneous data and inserted it into a MySQL database. But, my program got an error when it was inserting the population data for Bay St. Louis, Mississippi.

Apparently, the US Census team was so uncertain of the post Katrina population in Bay St. Louis they used a “Z” in place of a number for 2006 and 2007. They did the same for Pass Christian, Long Beach and Waveland. So then I decided to look at their estimates for nearby cities.

City 2005 2006 2007
Biloxi 50,614 44,624 44,292
Gulfport 73,340 65,296 66,271
D’Iberville 8267 7285 7423
Ocean Springs 17,573 16,813 17,246
New Orleans 453,726 210,198 239,124

The population decrease in New Orleans has been written about quite a bit, but it is still startling to see the numbers next to each other. I don’t remember seeing population data for the Mississippi coastal cities.

I had been looking for an excuse to play around with the Google Charts API. Here’s a graph of the population decreases for some of the cities.

Population decrease for Gulfport, Biloxi, and Ocean Springs

2/19/2008: 12:23 am: RobertMac, MySQL, Python

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.