Getting MySQLdb For Python Working on Snow Leopard
I'm just getting into Python and Django programming so I've been tinkering with the tutorials online to get familiar with it. I develop on a Macbook running 10.6.x and the pre-installed Python was working just fine for me until I needed to connect to MySQL for some of the Django work. To do this you need MySQLdb for python installed. I'm new to setting up Python environments and I'm sure there is a way to get Apple's Python working with MySQL but it just was not working for me, so after many, many failed attempts, I gave up and started developing Python on an Ubuntu VM on the Macbook (gotta love apt-get).
Some co-workers at Mozilla then turned me onto a new(ish) package manager for OSX called HomeBrew. Figuring that Apple's 'special' python was the root of my problems above (I've been down that road with apache and PHP), I thought I'd replace Apple's python with one installed using Homebrew and attempt the MySQLdb install again.
Here is a summary of that process.
Install Homebrew:
Recursively change the ownership of /usr/local to your user (not the most secure thing to do but I am comfortable with it on my dev laptop. Keeps you from running brew as root)
Run the installer:
Adjust your PATH to get /usr/local/bin at start of path so brew installed is picked up first
I did this by adding the line below to ~/.profile
Now you can start installing with brew:
You will need setuptools to install MySQLdb
Install 64bit Mysql. Folks accomplish this in many different ways, from src, binaries, I believe you can event brew install it. I originally had the 32-bit binary installed (from http://dev.mysql.com/downloads/mysql/) and was unable to get MySQLdb working with that. So I just downloaded and ran the install for the 64bit version binary.
Now on to MySQLdb for Python, download it at http://sourceforge.net/projects/mysql-python/
Follow the install in the README file which should be something like this:
Then test
As is often the case in Linux/Unix the 'no output' you get after import MySQLdb is the good news you were looking for. You are most likely all set to use Python and MySQL together now.
$ curl http://gist.github.com/raw/323731/HEAD/install_homebrew.rb -o /tmp/install_homebrew.rb
$ sudo chown -R $USER /usr/local
$ ruby /tmp/install_homebrew.rb
export PATH="/usr/local/bin:$PATH"
$ brew install python
$ brew install setuptools
$ tar xfz MySQL-python-1.2.1.tar.gz $ cd MySQL-python-1.2.1 $ # edit site.cfg if necessary $ python setup.py build $ sudo python setup.py install # or su first
$ cd
$ python Python 2.6.5 (r265:79063, Apr 27 2010, 12:36:14) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>


Comments (0)
Add a Comment