Tags

, , ,

Excuse me while I vent for a while. I’ve just spent an age trying to get Python talking to MySQL via MySQL for Python. Which for unknown reasons seems to be a pain in the backside if you’re on a Mac. I just wanted to be like the cool kids and try Django…

Ok enough of the nonsense. This is intended to help you if you’re having trouble getting MySQL-python installed or don’t know what to do when you get the message “No module named MySQLdb”. There seems to be a million and one articles out there about setting this combo up most of them seem to work for some people but not all. Hopefully I’ll cover a couple of cracks with this. Your amount of millage will vary depending on how similar your setup is to mine so for the record:

  • OS: OS X Snow Leopard
  • Python version 2.6.4 (32 bit)
  • MySQL 5.1.44 (32 bit)
  • MySQL-python-1.2.3c1
  • Xcode installed from the Snow Leopard disk with 10.4 support.

1) If you have a 32bit version of Python you will need to use the 32 bit version of MySQL to compile against. Vice versa for 64 bit.

2) You may also need setuptools. Follow these instructions.

3) Grab MySQL-python you’ll want the .tar version. Unzip/untar it and open “setup_posix.py” from the directory containing  MySQL-python.

4) Find the line starting with “mysql_config =” and replace the whole line with:

mysql_config.path = “/usr/local/mysql/bin/mysql_config”

Save and close it.

5) Create this symobolic link:

$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

6) Now in a terminal make sure you are in the MySQL-python directory and run this:

$ python setup.py clean

7) Now for 32bit versions::

$ python setup.py build

You should notice some folders created in the build directory. If you’re building 32 bit versions they’ll have the word fat in the name. You might get a warning about wrong versions or something, it seems ok to ignore it.

8) Now run:

$ sudo python setup.py install

For 64bit versions:

ARCHFLAGS="-arch x86_64" python setup.py build

You should notice some folders created in the build directory. If you’re building 64 bit versions they’ll have the word 86_64 in the name.

Now run:

sudo ARCHFLAGS="-arch x86_64" python setup.py install

You should see a message about it coping the .egg file.

You should be be done now. Check it by running python and import MySQLdb. You may get a warning first time but not an error.

If you get the error:

ImportError: dynamic module does not define init function (init_mysql)

Then you are probably mixing 64 bit and 32 bit versions of python and MySQL.

I found these sites useful for getting this sorted: http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/ , http://websaucesoftware.com/blog/?p=461 , http://davidmichaelthompson.com/2008/02/18/notes-installing-django-python-mysql-on-os-x/

If you’re involved with Django’s development I’d really recommend you throw some weight behind getting a decent Mac solution for MySQL driver installation. At the moment this is horrible. There must be countless people who’ve given up on the framework because they couldn’t get past the first page of the beginners tutorial.

About these ads