I am running Mac OSX 10.6 (Snow Leopard) on my home machine and had previously been chugging along just fine with my Python 2.5 and MySQL 5 setup. However, this morning I decided to try and get MySQL to play nicely with my Python 2.6 installation and I ran into all sorts of problems. I searched online for solutions and found that a ton of other people had problems as well, but none of their solutions worked for me. I did get mine working after a couple of hours of fussing and all of the problems ended up being the result of incompatible architectures.
I'll outline the steps I would take to get this running again on my machine and why. Here is the target setup:
- MySQL 5.1, 32 bit
- OSX 10.6.2, 32 bit
- Python 2.6, 32 bit
First, I'm going to remove my previous MySQL installation. But even before that, we obviously need to back up our database. Here's how we would create a dump of it if we have the mysqladmin installed.
$ mysqldump -u username -p -v databasename > databasename.sql
Next, we need to thoroughly uninstall MySQL:
$ sudo rm -rf /usr/local/mysql*
$ sudo rm -rf /var/db/receipts/com.mysql.mysql
$ sudo rm -rf /private/var/db/receipts/com.mysql.mysql*
Now that we've removed our previous install, we'll go to MySQL's download page and download the 32 bit .dmg package. Installing this is as easy as double clicking it after the download finishes.
Once we have MySQL installed and working fine, we need to install the MySQL-Python bindings. We'll go to the download page, and download the most recent version. Once you have this installed, cd into the directory and build it using the correct architecture flags.
$ ARCHFLAGS='-arch i386' python setup.py build
$ sudo ARCHFLAGS='-arch i386' python setup.py install
Finally, we need to ensure that we are using the 32-bit version of python. One way to tell if you are using the 32-bit or 64-bit version is if you enter your python interpreter and type:
If the output is 9223372036854775807 then you are using the 64-bit version of Python. If it is 2147483647 then you are using the 32-bit version.
The way to force your system to use the 32-bit version is to use the command:
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes
I added this to my .bashrc and then refreshed my terminal by typing:
$ source ~/.bashrc
Other alternatives to forcing the 32-bit Python are listed here.
Having done all that, you should be up and running! Hopefully this saves someone time, and if not, hopefully it will at least be a useful refresher for me next time I need to do something similar.
UPDATE: It turns out this was not the end of my problems since I also am using virtualenv to isolate my Django project installations. With the default way virtualenv is set up, it inherits a python interpreter from somewhere that does not take into account the versioner flags we defined. Unfortunately, the default Snow Leopard installation comes with python in 64-bit mode, even if you are running OSX in 32-bit mode and thus, this is the python interpreter loaded into your virtual environment. After a lot of work and fiddling with things, I found the way to get around this is to do the following:
Install Python from a .dmg which you can download on the Python website. The .dmg package from the Python site is the 32-bit version so this is perfect for us in this case. Mine installed by default into the directory /Library/Frameworks/Python.framework/Versions/2.6/bin/python
When you create your virtual environment, specify which python interpreter you would like to use. For me, this was:
$ cd /srv/python-environments/
$ virtualenv --python=/Library/Frameworks/Python.framework/Versions/2.6/bin/python new-environment
Also, if you need to check on which version of python is being called at any given moment, the following two commands will give you useful output.
$ which python
$ python --version