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

[code]
$ locate mysql_config

/usr/local/mysql-5.6.16-osx10.7-x86_64/bin/mysql_config

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

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

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

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:

[code]
$ ~/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
>>>
[/code]

 

Update

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:

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

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

5 thoughts on “Python 2.7, Django, and MySQL on OS X

  1. Tux

    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. James Reuben Knowles Post author

    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. Filipino Web Developer

    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. James Reuben Knowles Post author

    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.

Leave a Reply