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

This entry was posted in Mac, Programming and tagged , , , . Bookmark the permalink.

5 Responses to Python 2.7, Django, and MySQL on OS X

  1. Tux says:

    I am using the latest version of python (actually I just installed last night). And do you have any idea about getting py and mysql talk with each other. I can seem to find the bridge of these two. I tried several tuts about python mysqldb, also installed the mysql client. But when I tested to import mysqldb. it still throws an error.

  2. I use PyCharm and set up a Python virtual environment for each project to avoid polluting the machine with a plethora of Python packages.

    I’m using the MySQL-python package.

  3. ok, i missed that point about setting up a virtual env. I came from a PHP background and wanted to try out python for my new project. It seems like it’s quite hard to find legit notes about connecting the latest version of py, django, mysql under yosemite. Btw, the ‘>’ signs are converted to html entities.

  4. It was a bit of a shift for me also. I came from a LAMP and .NET background.

    I’ve found that a lot of problems disappear if one sets up Python virtual environments for development on OS X.

    Thanks for the heads up about the HTML entity problem. WordPress and code has a difficult relationship.

  5. Yea, I think so also. But previously I used Cryon syntx highlighter which was much better than that of google’s plugin. But anw, thanks for your tips

Leave a Reply