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.
Nat said:
I could install MySQLdb as described. The weird thing is, it only works if I start the Python-shell as root! If I try to import MySQLdb as an ordinary user (even user with admin-privileges), I get the error-message:
Traceback (most recent call last):
File “”, line 1, in
File “build/bdist.macosx-10.3-fat/egg/MySQLdb/__init__.py”, line 19, in
File “build/bdist.macosx-10.3-fat/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.3-fat/egg/_mysql.py”, line 3, in __bootstrap__
ImportError: No module named pkg_resources
Any ideas?
Andrew Spratley said:
Do you have setuptools installed correctly? What version of Pytong are you running? Also if you have multiple versions of Python installed check to see if you are running the same version under each user.
Nat said:
Setuptools should be installed correctly. At least I never had problems before. Currently I’m using Python 2.6.4. And yes, every user starts python 2.6.4., same built, basically everything the same.
Mark Johnson said:
Cool Thanks for this post. I am just starting python and this is a big help.
Eric Wilson said:
Almost there. At #7 (setup.py) I get:
Compiling with an SDK that doesn’t seem to exist: /Developer/SDKs/MacOSX10.4u.sdk
Please check your Xcode installation
gcc-4.0 -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g -bundle -undefined dynamic_lookup build/temp.macosx-10.3-fat-2.6/_mysql.o -L/opt/local/lib -L/opt/local/lib/mysql5/mysql -L/opt/local/lib -L/opt/local/lib/ -lmysqlclient_r -lz -lm -lssl -lcrypto -o build/lib.macosx-10.3-fat-2.6/_mysql.so
ld: library not found for -lbundle1.o
collect2: ld returned 1 exit status
ld: library not found for -lbundle1.o
collect2: ld returned 1 exit status
lipo: can’t open input file: /var/folders/af/af3g4Zw8HBCSiJQzlmT3RU+++TI/-Tmp-//cc5vYCNv.out (No such file or directory)
error: command ‘gcc-4.0’ failed with exit status 1
I have no idea what to make of this. Any ideas?
Andrew Spratley said:
@eric did you install xcode from the snow leopard install disk? When you install you should check the 10.4 support option.
Neum said:
You are a saint! Worked for me like a charm on 10.5
Thanks a ton for posting this. I was stuck on the mysql_config: command not found error when trying to build.
Mark said:
THANK YOU for your CLEAR directions. I have spent all afternoon working on this and nothing has helped despite the many instructions on this clear debacle by mysql and django.
It is great to say upfront that 32 bit python needs 32 bit mysql. Makes total sense in retrospect. But the init_mysql error has been driving lots of people nuts. Simply saying this probably means that you have nonmatching python and mysql installations did it for me. I had tried 64 bit mysql in an attempt to get this to work, etc, and I had mixed and matched everything. Thanks!!!! You’re right: mysql and python, get your act together!
Pingback: DataConnect.be Blog » Blog Archive » Compiling MySQLdb for Python on mac – djeez !
Pingback: Installing MySQLdb for Python on 64 bit Snow Leopard - Run To The Future
lg said:
I gave up and got a laptop with Ubuntu on it.
It is a horrid solution – why can’t somebody get this sorted out?
I was using MAMP and had lots of issues that in the end I thought would be easier to solve by adopting a completely new OS.
It is so annoying.
L
wb said:
Thanks a lot, this worked (once I installed xcode)
Vlad said:
this is wrong recommendation.
for Mac OSX 10.6 use MySQL x86_64 .dmg from mysql.com
setuptools are not needed.
read http://peak.telecommunity.com/DevCenter/EasyInstall#mac-os-x-user-installation
then just install mysql-python as descrubed in README. you may do it without sudo.
Craig Schmidt said:
There’s a typo here:
4) Find the line starting with “mysql_config =” and replace the whole line with:
should be
4) Find the line starting with “mysql_config.path =” and replace the whole line with:
Pingback: Alexander Simoes: Journal – Install MySQLdb 1.2.3 on Mac OSX Snow Leopard (10.6)
Clayton Cafiero said:
You have no idea how much this helped. Now if only I could iron the dents out of my forehead.
THANK YOU.
savey said:
I used these directions and I was up and running over the summer with both 32-bit versions. Unfortunately I’ve had to reinstall MySQL since then and I’ve upgraded python. I’m running MySQL 5.5 and Python 2.6. I’ve checked and both are 64-bit versions. Now I’m in the same mess as before and I get the:
ImportError: dlopen(/Users/stefanavey/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-fat.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/Users/stefanavey/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-fat.egg-tmp/_mysql.so: mach-o, but wrong architecture
I followed the directions above with the architecture flags but the file names still have fat in them and I get the above error message in python when trying to import MySQLdb.
Has anyone been successful with 64-bit versions?
MikeK said:
I get exactly the same ‘wrong architecture’ results – has anyone figured out where to go from this point?
Jason said:
Running on mac osx 10.7, python 2.7, 64 bit version, MySQL 64 bit version as well.
I followed along step by step and seemed to good progress but caught a snag where instructed to run:
$ ARCHFLAGS=”-arch x86_64″ python setup.py build
which returns a series of successful outputs but ending with:
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -O3 -Dversion_info=(1,2,3,’final’,0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.6-x86_64-2.7/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
unable to execute gcc-4.2: No such file or directory
error: command ‘gcc-4.2′ failed with exit status 1
Then running:
$ ARCHFLAGS=”-arch x86_64″ python setup.py install
Similarly fails with:
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -O3 -Dversion_info=(1,2,3,’final’,0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.6-x86_64-2.7/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
unable to execute gcc-4.2: No such file or directory
error: command ‘gcc-4.2’ failed with exit status 1
I proceeded to try and import the module as well, which throws the import error:
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
Traceback (most recent call last):
File “”, line 1, in
File “MySQLdb/__init__.py”, line 19, in
import _mysql
ImportError: No module named _mysql
Not sure if this is related to the preceding build/install failures.. details on the web around “ImportError: No module named _mysql” are fairly scarce. I’ve been racking my brain on this for some time now, any pointers would be *greatly* appreciated.
Andrew Spratley said:
The problem would apear to be in the line: “unable to execute gcc-4.2: No such file or directory”
Did you install xcode? Can you find gcc-4.2 on your system?
Jason said:
Thank you for the prompt response and apologies for the latency with mine..
I have installed Xcode (though had done so after after the fact), a search doesn’t find any direct file “/gcc-4.2” but does find a number with it in the name:
Jason-Beasleys-MacBook-Pro:MySQL-python-1.2.3 jaybeaz$ sudo find / -name *gcc-4.2*
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/i686-apple-darwin10-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/arm-apple-darwin10-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/i686-apple-darwin10-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/Developer/usr/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/Developer/usr/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/usr/bin/i686-apple-darwin11-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/usr/bin/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/i686-apple-darwin11-llvm-gcc-4.2
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
Any ideas where may have gone wrong here? should I symlink one of these to MySQL-python folder before attempting the build? Thinking of dumping all of these and starting again from scratch with Xcode being installed beforehand.
Jason said:
So I ended up downloading a separate gcc package I found on github after researching a little more and afterwords was able to complete the build and install as described above. After many hours of passive and focused research, trial and error, Python and MySQLdb now work on my Mac. Thanks Andrew!
John said:
Hi,
I was sent this link by a friend who knew I was struggling too. I followed the instructions and all seemed to go well, but when I try to run my python code, I get the following error message: ImportError: no module named ‘MySQLdb’.
I am running MySQL via MAMP and python with Aptana Studio 3.
Any thoughts?
Chris Berg said:
Following these directions, but trying to install from a sage shell. I’m getting the following error:
gcc -fno-strict-aliasing -fwrapv -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,4,’beta’,4) -D__version__=1.2.4b4 -I/usr/local/mysql/include -I/Applications/sage/local/include/python2.7 -c _mysql.c -o build/temp.macosx-10.7-intel-2.7/_mysql.o -Os -g -fno-strict-aliasing -arch i386
gcc: error: i386: No such file or directory
gcc: error: unrecognized option ‘-arch’
error: command ‘gcc’ failed with exit status 1
Tony said:
Thank you for these notes. Worked seamlessly, like a charm. Why can’t all documentation be like this?
Seb said:
This was spot on, thank you very much! I did have one small issue where I was receiving the error:
“ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.9-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib”
I followed the instructions at: http://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib
And found the symlink solution worked best for me: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
So in all, to get this working I did (for 64 bit OS X 10.9, with Xcode already installed):
—————————
Install MySQLdb
—————————
– get tar from http://sourceforge.net/projects/mysql-python/
cd Downloads/
tar -xvf MySQL-python-1.2.4b4.tar
cd MySQL-python-1.2.4b4
export DYLD_LIBRARY_PATH=/usr/local/mysql-5.6.15-osx10.7-x86_64/lib/:$DYLD_LIBRARY_PATH
ARCHFLAGS=”-arch x86_64″ python setup.py build
sudo ARCHFLAGS=”-arch x86_64″ python setup.py install
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Jen said:
After a week of scouring the internet to figure out why my setup wasn’t working, this reply finally worked for me. Thank you a million times for this one line of code that kept me from throwing my computer out the window.
Sentla said:
Procedure works great! I got a link error after performing the steps above. I had to link the Library manually:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
Ben said:
Thank you so much for this. Been banging my head for hours trying to get this to work. Might have saved the project I’m currently working on!