Tag Archives: python 2.7

Python 2.7, Django, and MySQL on OS X

For some reason, getting Python and MySQL talking on OS X has been an annoyance. These are my notes for getting the two to talk to each other in a Python 2.7 virtual environment for a Django project.

The Django 1.6 docs recommend using the MySQLdb package. Its installation uses the mysql_config executable.

I have the following set up:

  1. MySQL 5.6.16
  2. Python 2.7.3
  3. PyCharm 3.1.1, which was used to create
  4. a Python virtual environment with pip located in $HOME/upharm27

$ locate mysql_config


$ export PATH=$PATH:/usr/local/mysql-5.6.16-osx10.7-x86_64/bin
$ ~/upharm27/bin/pip install mysql-python

I have gcc 4.7.2 available, but curiously, the installer gave the following message:

Installing collected packages: mysql-python
Running setup.py install for mysql-python
gcc-4.2 not found, using clang instead

The install succeeded using clang, so it’s nothing more than a curiosity at this point.

I was able to verify that the package was installed with:

$ ~/upharm27/bin/python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb



I just followed these notes using the latest XCode 5.1 on OS X 10.9 and the mysql-python install failed. Apparently the clang Apple ships produces errors on unknown flags by default. I was able do get around this by:

$ export CFLAGS=-Qunused-arguments
$ export CPPFLAGS=-Qunused-arguments
$ ~/upharm27/bin/pip install mysql-python

Thanks to the good folks at StackOverflow. More details may be found here.

Python 2 to 3 Conversion Notes

A project that I’m working on is being produced with the intention that it will be in production for a very long time — 10+ years. Thus converting from Python 2.7 to Python 3.4 seems to be a wise option to explore.

These are some personal notes regarding the differences between the two versions, supplementing the excellent articles out there. These are based on examining diffs after running 2to3 on the code base.

xrange and range

Contrary to what is implied in many articles, the separate functionality in xrange and range has not been mysteriously merged.The Python 2.7 xrange has been renamed to range. The Python 2.7 range is gone completely. To get the same effect, use

[python light=”true”]

foo = list(range(1, 10))



The 2to3 code tool did this correctly; seeing its effect merely clarified how Python 3 works.


I had to manually go through my objects and straighten out __str__ and __unicode__. This involved:

  1. Deleting __str__ methods which essentially duplicated __unicode__.
  2. Renaming __unicode__ to __str__.


I used __long__ in one object, and simply deleted it since under Python 3 it is a duplicate of __int__.

long() and int()

Similar to __long__, there was one instance where I simply deleted the code associated with long().


I had to manually convert the Python 2.7

[python light=”true”]

super(ClassName, self)



[python light=”true”]




2to3 attempts to convert map() statements to list comprehensions. It did well on most, but screwed up in two places. It might be wise to search for leftover map() statements.


14 May 2014
Added notes for map().