I'm a Python newbie, but I've just spent a day working out how to get MySQLdb working properly, and the universe according to google includes numerous references to what a PITA it is, and an inordinate number of guides that seem to be outdated. Given that this site is intended to address these sorts of problems, and I know that I'm going to need a reference to the solution in future, I'm going to ask the question, provide my answer and see what else floats to the surface.
So the question is how to get MySQLdb working on Mac OS X?
Here's another step I had to go through, after receiving an error on completing Step 9:
Reference: Thanks! http://ageekstory.blogspot.com/2011_04_01_archive.html
Just had this problem (again!) after getting a new Lion box.
Best solution I've found (still not 100% optimal, but working):
make sure you have 64-bit python. How to check if a library is 32bit/64bit built on Mac OS X?
install easy_install if you don't have it. http://packages.python.org/distribute/easy_install.html
install GCC if you don't have it.
you can get it by downloading XCode/Dev Tools from Apple - this is a big download -
... but instead I recommend this github which has what you need (and does not have XCode): https://github.com/kennethreitz/osx-gcc-installer
I downloaded their prebuilt PKG for lion, https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg
make sure you have downloaded a 64-BIT version of MYSQL Community. (The DMG install is an easy route) http://dev.mysql.com/downloads/mysql/
Set paths as follows:
export PATH=$PATH:/usr/local/mysql-XXXX
export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/
export ARCHFLAGS='-arch x86_64'
NOTE THAT:
Install the beast!
easy_install MySQL-python
LAST STEP:
Permanently add the DYLD_LIBRARY_PATH!
You can add it to your bash_profile or similar. This was the missing step for me, without which my system continued to insist on various errors finding _mysql.so and so on.
export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/
@richard-boardman, just noticed your soft link solution, which may in effect be doing the same thing my PATH solution does...folks, whatever works best for you.
Best reference: http://activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/
should fix the issue for you as the system is not able to find the mysql_config file.
Install mysql and python via Macports The porters have done all the difficult work.
should install what you need. (see Stack overflow for comments re mysql server)
If you only need to connect to mysql and not run a server then the first line is sufficient.
Macports now (early 2013) will provide binary downloads for common combinations of OS a executable architecture, for others (and if you request it) it will build from source.
In general macports (or fink) help when there are complex libraries etc that need to be installed.
Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.
On macos Sierra this work for me, where python is managed by anaconda:
anaconda search -t conda mysql-python
anaconda show CEFCA/mysql-python
conda install --channel https://conda.anaconda.org/CEFCA mysql-python
The to use with SQLAlchemy:
If you are using 64 bit MySQL, using ARCHFLAGS to specify your cpu architecture while building mysql-python libraries would do the trick: