Update: This entry has been updated for Snow Leopard.
In all of Mac-dom, there are few experiences more painful than trying to get Python tools to talk to a MySQL database. Installing MySQL itself is easy enough – Sun provides a binary package installer. Python 2.5 comes with Mac OS X. If you enable Apache and PHP, your PHP scripts will talk to your installed MySQL databases just fine, since PHP comes bundled with a MySQL database connector. But try to get up and running with Django, TurboGears, or any other Python package where MySQL database access could be useful (or needed), and you’re in for a world of hurt.
Update: I finally did manage to get Python and MySQL playing nice together, but it took a few more contortions beyond what’s described in the recipes found scattered around the interwebs. I’ve added my solution at the end of this post.
First, the official mysql-python package available at SourceForge does not offer a .pkg installer. No problem – we can build from source. Ah, but the source code is developed for and tested on Linux, and has apparently never been test-built on Mac OS X by the maintainers.Â The usual
python setup.py buildsudo python setup.py install
will not work. Search for “mac python mysql binding” and you’ll find dozens (hundreds?) of sites describing processes you can go through to edit a few of the source files so they’ll compile on OS X. Some of them work, some of them don’t. I spent three hours yesterday trying one recipe after another. I tried mysql-python versions 1.2.1, 1.2.2, and the beta version of 1.2.3. The recipes that would get python-mysql to build and install still failed when I attempted
import MySQLdb from within Python. Complaints of the same problem largely go unanswered – presumably people just give up.
easy_install is no help either – it chokes because it can’t find the path to
mysql_config, and there’s no way to hand it one (that I can tell.)
Judging by timestamps on old blog entries, this sorry state of affairs has been going on for years. Developers wasting hours trying to get a simple db connector to compile… but it never seems to get fixed. “It’s open source,” you say – “Fix it and contribute your fixes back to the community.” I would. I’d love to. But I can’t get the damn thing to compile and install cleanly, despite all the recipes out there. Even seasoned developers in #djangoÂ offer responses like “Yeah, I remember that was a huge PITA. I eventually got it to compile, but don’t remember how.”
There is another option – a precompiled python-mysql connector does exist, but only available through the MacPorts project, which aims to remove some of the pain of installing common *nix tools on OS X. But there’s a rub – the connector available through MacPorts only ships with the complete MacPorts version of MySQL. So unless you want to avoid the confusion of having two RDBMs running at the same time, you’ll need to rip the official MySQL (and its startup item) out of your system before you begin. Of course, if you’ve already got data stored in those databases, you’ll need to back up that data first. Even then, getting the MacPorts version of MySQL up and running is no picnic either. You’re still going to end up with a lot of post-install configuration, tweaking, permissions issues, etc. This shouldn’t be necessary. There should be an installer that works with Sun’s stock MySQL dist and Apple’s bundled python. You shouldn’t need to alter your whole system config for this one little piece.
You have to wonder how many people have gotten curious about Python development, hit their heads on this brick wall, and thrown up their hands. As Keenan Thomson would say, “FIX IT!!!”
Someone out there knows how to create a mysql-python package that’s as easy to install as MySQL itself. We need to identify that person, offer them some bread for their trouble, and get this problem fixed for once and for all.
Note: I am no longer running a tipjar to get this work done. For whatever reason, we’re not seeing many contributions, and I’ve got the problem solved now (painful as it was). If you’d like to take up the mantle, go for it.
On the other hand, if you’re a web developer who’s tired of banging your head against this ridiculous wall, please leave a few bucks for the developer who promises to solve the problem for once and for all. When deciding how much to commit, consider your regularly hourly rate, then estimate how many hours you’ve spent trying to get over this hurdle. In other words, I’m guessing that having this fixed is probably worth more to you than $5… but whatever you can contribute is appreciated. I’ll manage the collection and pledge to turn the whole sum over to the first developer who can package up a py25-mysql database connector that “just works” with Sun’s stock MySQL for OS X and Apple’s python 2.5. Users should not need to download 1GB of XCodeÂ just to get a compiler or jump through any other hoops. Just a simple installer (CLI OK, but .pkg preferred).
OK, so I finally did solve this one. I’ve actually been trying to configure two machines at once, and running into different problem sets — the usual recipes (this is my favorite, but do step #6 before step #0) did work for one machine, but not for the other. One one machine, I thought it wasn’t working because of error messages returned when trying to
import MySQLdb from a Python shell. Turns out you can ignore some of these errors and things will still work from within a Python app.
On the other machine, the problem went deeper. I got errors when trying to
import MySQLdb, but they were different :
Error loading MySQLdb module: dynamic module does not define init function (init_mysql)
I’m not sure what was different about the two machines, but it was a showstopper. Here’s how I finally solved it.
First of all, don’t use any of the instruction sets you’ll find online for building/installing the mysql-python binding. If you’re getting the “does not define init function” error, the usual recipes won’t work for you (no, I don’t know why – they just won’t).
1) Download, install, and configure the MySQL server dmg from Sun, along with the StartupItem and PreferencePane. Get MySQL itself up and running, with root permission established, etc.
2) Install MacPorts, then use it to retrieve the whole python-mysql package:
/opt/local/bin/port install py25-mysql
This will take a while – it unfortunately is going to compile/install a complete mysql server, along with the bindings you need. However, it won’t set it to launch on boot or anything, so it won’t mess with anything on your system.
3) All we need are the properly compiled bindings from the py25-mysql package, which we’ll copy to the system’s site-packages dir. So:
$ cd /opt/local/lib/python2.5/site-packages # Copy two dirs and three files to system's site-packages dir sudo cp -r MySQL_python-1.2.2-py2.5.egg-info /Library/Python/2.5/site-packages sudo cp -r MySQLdb /Library/Python/2.5/site-packages sudo cp -r _mysql* /Library/Python/2.5/site-packages
4) You can now start a Python shell and enter
import MySQLdb with no errors. Yay! But… you still won’t be able to connect to your server from a configured Django project — it’ll complain about not being to find a mysql socket file at
/opt/local/var/run/mysql5/mysqld.sock. Not sure what causes that, but it’s easy to fix. In the
settings.py for your Django project, specify a path to the socket file as the DATABASE_HOST:
DATABASE_HOST = '/tmp/mysql.sock'
You’re in business.
SNOW LEOPARD UPGRADE NOTES
Good news: Snow Leopard upgrades Python to 2.6.1. Bad news: It’ll break your MySQL installation as well as your Python-MySQL database connector. Fortunately, getting things working is a bit easier than it was under Leopard, and you won’t need to rely on MacPorts at all..
I had an older 32 bit version of MySQL installed. With a bit of futzing I was able to get my old MySQL installation running again, but could not get the python-mysql binding working. Finally installed the latest 64-bit version from mysql.com. Before installing, be sure to export the databases you want to keep first! My databases were blown away by the upgrade, and I had to restore them. Get your databases reinstalled, privileges established, and make sure you can connect to your mysql server normally.
To reinstall the Python MySQL connector you’ll need the gcc compiler, which you’ll also find has been blown away in the Snow Leopard upgrade. To get gcc reinstalled, run the Xcode installer from the Snow Leopard DVD.
Now grab the latest version of the python-mysql connector (check to make sure that’s the most current download URL when you read this).
Unpack the archive and cd into its directory. Here’s the key: You’ll need to specify your 64-bit architecture during the compile process:
ARCHFLAGS='-arch x86_64' python setup.py build ARCHFLAGS='-arch x86_64' python setup.py install
If you get the error “mysql_config not found” follow the instructions here, which are essentially this: Open up site.cfg from the binding distribution and make sure it has this (the path to mysql_config should match the actual one on your system):
mysql_config = /usr/local/mysql/bin/mysql_config
Save the file, try the build again, and it should work.
You should now be able to launch a Python shell and type:
>>> import MySQLdb
and get no errors. And your Django projects should work just fine. If you get an error that the shared library (the binding .so) couldn’t be found, you probably need to export the DYLIB path. Try this in a shell before starting Python:
Then try importing MySQLdb again. If it works, add that line to your .profile.
97 Replies to “Python-MySQL Connections on Mac OS”
Thank you very much you saved me ungodly amounts of heartache
Wow, thanks a million! Or at least 997,412. I had no trouble installing this years ago, but, alas, can’t remember how! I sound like a quote from your article.
Anyway, the reason people work so hard at getting this to work is, of course, how much you can do once you get it working! I’ve got dozens of little python scripts that whale on my mySQL databases and do marvelous things.
Thank you thank you thank you so much!!! All the other recipes out there weren’t working for me, this was perfect.
I love you.
I love you too, Doug!
I’ve installed Mysql through mac ports – and it works fine.
Will i need to uninstall this? I really want to get Django and Mysql working…
Gareth – If you can start a python shell and type “import MySQLdb” with no errors, and your database is working fine, then no reason to go back. These instructions are really for people who have already set up the official mysql and don’t want to have to switch to the macports one just to get the connector working.
Thanks, works pefecto! :)
Thanks! Kept me busy for three hours but this worked !
Thanks so much…process was very smooth. This was a real timesaver for me….no more headbanging.
This post may have just saved my life. Or at least my career. Definitely my sanity.
I spent two days trying to get Python/Django/MySQL to work on my Mac the first time around — and then spent two long nights trying to put it all back together again after an ill-fated attempt to install PostgresSQL broke everything. (If you could ever write a SQL sequel on how to get psycopg to work on a Mac, I guarantee it would be an even bigger hit.)
Anyway, on behalf of data journalism, struggling writers-turned-programmers and Mac lovers everywhere, thankyouthankyouthankyou.
I don’t do postgres, but glad you found this useful. Judging by the comments, I can see that I’m not alone in my feelings about this screwed up situation. Just amazing that the problem has lasted this long.
Thank you so much. Finally can import MySQLdb.
But, I cannot get a connection:
_mysql_exceptions.OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)”)
Peter, did you see the end of the tip in the original post?
Again, thanks so much for the post – but I have no immediate interest in “Django” so using the “settings.py” to set the socket is irrelevant as is setting DATABASE_HOST.
Furthermore I cannot simply copy /tmp/mysql.sock to /opt/local/var/run/mysql5/mysqld.sock (I get “Operation not supported on socket”).
Peter – I’m not saying you need to be using Django, I’m saying that whatever app or util you’re using to connect to mysql must have a DATABASE_HOST or HOST setting somewhere…
After looking at _mysql.c, I used the following with success:
So, thanks much again.
You are an absolute lifesaver. I spent several hours doing everything I thought I could do… and finally your solution worked.
This is so cool. Thanks so much for writing this. I am *so* bookmarking this.
I was getting that same init_mysql error. I finally realized I had installed the 64-bit version of MySQL. Once I had the 32-bit version of MySQL installed, I ran python setup.py clean –all to clean my last build then I reran all the MySQL_python install commands. Everything worked fine after that.
I’m new to Python so I’m uncertain if it is 64-bit ready.
You can check your installation running these two commands in Terminal:
If you see any libraries listed as “Mach-O 64-bit dynamically linked shared library x86_64” then you have the 64 bit version.
Great tips Jerry – thanks. Not sure if that was my issue though – I don’t show any trace of 64-bit libs, but still had the problem.
Than you sooo much! great tip, reported back to this site about your solution:
You’re the Master!
It seams impossible to do it without your explanation.
I’m running 10.6, and I’m downloading the MySQL package to install it now.
I was able to use the easy_install . command, as my MacPorts MySQL binaries were on my ZSH path (if they’re on your BASH path, it should work too) â€” which was at /opt/local/lib/mysql5/bin.
I’ll comment back once I test the MySQL installer (I’m on 10.6 but I’m downloading the 10.5 installer; they don’t seem to have a package for 10.6 yet).
On 10.6, I’ve got /usr/local/mysql/bin on my PATH, and I was able to run the easy_install . command, as it could find mysql_config without a problem.
It’s wrapped itself up in a nice little egg in my Python site-packages directory.
Running setup.py -q bdist_egg --dist-dir /Users/ben/Downloads/MySQL-python-1.2.3c1/egg-dist-tmp-_9FIBu
zip_safe flag not set; analyzing archive contents...
Adding MySQL-python 1.2.3c1 to easy-install.pth file
Processing dependencies for MySQL-python==1.2.3c1
Finished processing dependencies for MySQL-python==1.2.3c1
After that, I pulled up one of my Django projects that uses MySQL, and synced it. Works like a charm.
Benjamin that’s *great* news – thanks for that news. Makes me feel less squeamish about upgrading.
Here’s another solution from the SourceForge page for the project:
export ARCHFLAGS=’-arch i386 -arch x86_64′
python setup.py build
sudo python setup.py install
Seems to work fine.
Thanks for the information. However, I get now the following error when trying to import MySQLdb:
Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLdb
Traceback (most recent call last):
File “”, line 1, in
File “/opt/local/lib/python2.5/site-packages/MySQLdb/__init__.py”, line 19, in
ImportError: dlopen(./_mysql.so, 2): no suitable image found. Did find:
./_mysql.so: mach-o, but wrong architecture
Yeah, I investigated this issue a bit further and concluded that the _mysql.so library does get compiled as “Mach-O 64-bit bundle x86_64” and my Python is 32-bit = problem.
Is there some way to force that i386 architecture would be used instead? I found out that MacPorts has a configuration file in /opt/local/etc/macports/macports.conf that has a switch for determining the build architecture. It defaults to x86_64 in Snow Leopard but I forced it to i386. However, it didn’t affect at all, that library still appears to be 64-bit :(
I wonder if I am the only one here with Snow Leopard having the problem, is everybody else using 64-bit python?
duh – I haven’t tried this on Snow Leopard yet but my understanding is that SL comes with Python 2.6. Your trace above shows Python 2.5.4, indicating that you’re using using a different (MacPorts?) version of Python.
I’ll update these notes when I get SL, but if my reading is correct, the binding source compiles pretty clean. For now I recommend you search for sites that document the process for Snow Leopard.
Thank you master!
See Snow Leopard update notes above.
I followed the instructions to the letter…I even had to muck about with the stdarg.h file per this link:
and I finally (FINALLY!) got build and install to get all the way through, only to hit the following break:
brandon-watsons-macbook-pro:MySQL-python-1.2.3c1 blwatson$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import MySQLdb
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg/_mysql.pyc, but /Users/blwatson/Downloads/MySQL-python-1.2.3c1 is being added to sys.path
Traceback (most recent call last):
File “”, line 1, in
File “MySQLdb/__init__.py”, line 19, in
File “build/bdist.macosx-10.3-fat64/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.3-fat64/egg/_mysql.py”, line 6, in __bootstrap__
ImportError: dlopen(/Users/blwatson/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/Users/blwatson/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat64.egg-tmp/_mysql.so: mach-o, but wrong architecture
So I am at a loss…I have been wrestling with getting MySQL and Django working for days now. Your info has been immensely helpful in getting me to this stage, so thank you VERY much. However, I am at a roadblock…hopefully you know how to parse the above error.
Ugh, I tried reinstalling and am getting the dreaded mysql.sock error.
I have installed the 64bit version of MySQL onto Snow Leopard. I went to:
Mac OS X 10.5 (x86_64)
once it was installed. I then ran:
$ sudo ./MySQLCOM start
Starting MySQL database server
but then get:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2)
the mysql dir in /var is empty. I have also looked in /tmp. Nothing. I then ran:
$sudo find / -name mysql.sock
and nothing was found on the disk.
Lastly, I tried:
$ sudo mysqld_safe
091001 01:18:42 mysqld_safe Logging to ‘/usr/local/mysql/data/brandon-watsons-macbook-pro.local.err’.
091001 01:18:42 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
091001 01:18:44 mysqld_safe mysqld from pid file /usr/local/mysql/data/brandon-watsons-macbook-pro.local.pid ended
What do I do now?
Brandon, sorry to have not gotten back to you sooner. Life just got flooded. Did you get this solved? One thing I notice above is that you mention:
but you’re talking about installing on Snow Leopard.
You rock man! Thanks so much!
I’ve been trying many times to get it working and you just explained it great.
Thanks, you just saved my butt. I thought this was one of those issues I’d spend hours on.
Thanks. This really helped me out. I had spent two hours, and I supposed that if I hadn’t stumbled upon this, it would keep giving some trouble for awhile.
thanks++. This was really helpful.
I solved the problem “Error loading MySQLdb module: dynamic module does not define init function (init_mysql)” as follows:
– I listed all the contents from py26-mysql doing the following
# port contents py26-mysql
– and I added the following entry to /etc/profile
Good post!. I think you can add this as an alternative way :).
I spent the good part of a day battling this as well. Everytime I get a new Mac, I seem to stumble through this.
I was getting the “wrong architecture” error from the _mysql.so which was being built, even thought I was passing the 64-bit build args. I ended up switching to 32-bit MySql, and built MySQLdb with “-arch i386”. Voila…everything works….finally.
I think the reason why this has been so painful for so long is that the author considers OS X a “fringe operating system“. :>)
Heh – Well, he apparently considers Windows a “fringe operating system” too :)
Hey, thanks! I spent about an hour and a half beating my head against the wall trying to wrangle the release version up on sourceforge into working. Almost just went with another database I was so frustrated. Props and gratitude!
Thanks so much for this. After much frustration, this was able to get everything working.
For heaven’s sake man, put the tip jar back out!
Incredible that this is still such a clusterf**k after so long. Do most python/django developers use Postgres, or something?
pjm, I agree that it’s incredible. But nope, most python/django devs do use MySQL. We just wrestle through this and then hope the next OS update doesn’t break everything. File under “puts hairs on your chest.”
What can I say, except I was in a world of pain there for many evenings going round in circles, uninstalling, reinstalling completely removing mysql and the python mysql a number of times until I found this.
Thank you!!! This worked I just launched django successfully connected to mysql rather than mysqlite for the first time, wow thank you again!
thanks man you don’t know how many hours i spent on this :)
I have tried all possible solutions but this is still not working for me and I have no idea why. The last solution I tried was the SNOW LEOPARD one. I did all the steps. Erased my previous mysql (MAMP), reinstall the 64-bit architecture mysql (working fine now!) and run the xcode installer.
Then I did the steps you suggest but when I do: ARCHFLAGS=’-arch x86_64′ python setup.py build I get a huge list of errors:
copying MySQLdb/release.py -> build/lib.macosx-10.5-fat64-2.6/MySQLdb
building ‘_mysql’ extension
gcc -L/sw/lib -bundle -L/sw/lib/python2.6/config -lpython2.6 -arch x86_64 build/temp.macosx-10.5-fat64-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc -o build/lib.macosx-10.5-fat64-2.6/_mysql.so
ld: warning: in /sw/lib/python2.6/config/libpython2.6.dylib, file is not of required architecture
“_PyMember_SetOne”, referenced from:
…. (a lot more stuff here)…
and at the end
__mysql_server_init in _mysql.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
error: command ‘gcc’ failed with exit status 1
Please I have spent two days trying to solve this issue and no luck so far… any help will be strongly appreciated :-)
Daniel – Are you actually ON Snow Leopard, or did you try that technique on Leopard?
No, i am ON snow leopard… mac os X version 10.6.2 build 10C540 and when i run python i get Python 2.6.2 (r262:71600, Apr 28 2009, 20:43:17)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
and gcc –version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
not sure if that helps you but at this point I am quite hopeless :-(… thanks a lot shacker
Hmm… I wonder how you ended up with version 2.6.2 of Python. I’m on SL and have 2.6.1. I’d hate to think such a minor version difference would prevent compilation, but maybe there’s something non-standard in your Python install?
hmmmm not sure either… do you know a good way to uninstall python and install the version you have? where can I download the python version you are suggesting? i could try that and see if it works, at this point i’m clueless of what’s going on so may be worth trying…
If you type:
$ which python
what do you get? I get:
And if you run:
does it then show 2.6.1?
mmm if i type which python i get:
and there is nothing in the folder usr/bin/python
oooops sorry yes, you are right
if i run usr/bin/python I run Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
OK I think that’s your problem. You’ve installed a non-apple copy of python in /sw/bin – maybe from fink or ports? You should be able to find a line in your ~/.profile or or ~/.bash_profile that’s setting a shell path that finds that version before it finds the native version. Comment out that line, restart terminal, and try “which python” again. When it returns the correct path, go back and try compiling the driver from source again (you might have to “make clean” or just start with a fresh tarball).
wow! no comments!!!!! you are a life savior! :-) thanks you sooooooooo much…
Great to hear it Daniel. Go Django!
From the above comments, I can only what the joy must be for solution.
I am happy to see the last email comment was but 10 days ago.
Here is the beef:
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Mac OS X 10.6.3 Build 10D573 with Xcode 3.2.1 for Mac OS X 10.6 Snow Leopard.
I have mysql-5.1.45-osx10.6-x86_64 installed with its prefpane. “-$ which mysql” gives me “/usr/local/mysql/bin/mysql”. Works on the Command line as well as throughthe Workbench.
I have a number of test DBs that I want to pull from using Django.
So I download the MySQL-python-1.2.3c1 into ~/Downloads/Applications and navigate to this and run :-$ python setup.py build.
To which it tells me:
copying _mysql_exceptions.py -> build/lib.macosx-10.6-universal-2.6
copying MySQLdb/__init__.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb/constants
building ‘_mysql’ extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,’gamma’,1) -D__version__=1.2.3c1 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -g -Os -arch x86_64 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL
unable to execute gcc-4.2: No such file or directory
error: command ‘gcc-4.2’ failed with exit status 1
[… trimmed …]
Apart from understanding what the goblyguk above represents, what am I doing wrong?
Jimmy – I truncated your comment a bit. “No gcc” means you haven’t installed a compiler on your system. You need to download the developer tools from Apple so you have a compiler! Yes, it’s a 1GB download but it will come in handy for all kinds of things.
Shacker, you were right – again ;).
It was the Xcode installer. Any one who upgrades to Snow Leopard should upgrade Xcode, period.
This site is the best resource for this problem. Thanks again.
Great to hear it Jimmy! Now go rock the Django.
I managed to get this to work by bypassing macports, and using the XTools gnu C libraries. It’s the same when trying to install 3rd party modules on top of perl, which was 5 weeks of banging my head against a brick wall. I eventually had the epiphany that would have saved the day, but too late, I’d already ruined the leopard C libraries beyond repair. Alas, I divulge.
Before running python setup.py, add the two following environment variables (assuming you have XCode installed). In Terminal:-
Personally, I find it tidier to add symbolic links in /usr/lib and /usr/include pointing to these directories, and then point CFLAGS AND LDFLAGS to these symbolic links.
$ sudo ln -s /Developer/SDKs/MacOSX10.6.sdk/usr/lib /usr/lib/xtools
$ sudo ln -s /Developer/SDKs/MacOSX10.6.sdk/usr/include /usr/include/xtools
$ export LDFLAGS="-L/usr/lib/xtools"
$ export CFLAGS="-L/usr/include/xtools"
I think it’s shocking that I never read this recommendation on any forum anywhere. I just found many recommendations saying to install xcode, but they never said how to link the xcode C libraries for compiling programs from source in the terminal. I think this is the missing key for easily compiling source code consistently in Macs, without the use of a MacPorts.
I hope this works well for you too ;-)
I’m in a similar boat to a lot of people I guess. Here’s what happens when I try to run python manage.py syncdb:
Traceback (most recent call last):
File “manage.py”, line 11, in
File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 438, in execute_manager
File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 379, in execute
File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 257, in fetch_command
klass = load_command_class(app_name, subcommand)
File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 67, in load_command_class
module = import_module(‘%s.management.commands.%s’ % (app_name, name))
File “/Library/Python/2.6/site-packages/django/utils/importlib.py”, line 35, in import_module
File “/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py”, line 7, in
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File “/Library/Python/2.6/site-packages/django/core/management/sql.py”, line 5, in
from django.contrib.contenttypes import generic
File “/Library/Python/2.6/site-packages/django/contrib/contenttypes/generic.py”, line 6, in
from django.db import connection
File “/Library/Python/2.6/site-packages/django/db/__init__.py”, line 75, in
connection = connections[DEFAULT_DB_ALIAS]
File “/Library/Python/2.6/site-packages/django/db/utils.py”, line 91, in __getitem__
backend = load_backend(db[‘ENGINE’])
File “/Library/Python/2.6/site-packages/django/db/utils.py”, line 32, in load_backend
return import_module(‘.base’, backend_name)
File “/Library/Python/2.6/site-packages/django/utils/importlib.py”, line 35, in import_module
File “/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py”, line 14, in
raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/matthewmarcus/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/Users/matthewmarcus/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so: mach-o, but wrong architecture
That mach-o, but wrong architecture issue seems to be coming up for quite a few people. I’ve pored through the comments but I can’t work it out: has anyone come up with a simple fix for the problem?
This shouldn’t be this difficult !@$^!
I am running osx 10.6.4. I am using the python version that installed with the OS (which reports as 2.5). Downloaded 64 bit mysql v. 5.1 and have successfully used this with other projects (php).
When attempting to build the mysql driver gcc reports the following error.
cc1: error: unrecognized command line option “-Wno-long-double”
the -arch x86_64 option seems to be part of the build instructions, but adding it explicitly gets the same result. … I am still stuck.
Help, 2 days of grief trying to get this to work. I’m at exactly the same point as what this guy posted above:
Brandon Watson says:
September 30, 2009 at 10:47 pm
And following your instructions here:
March 19, 2010 at 10:14 pm
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
the only element that isnt mentioned above is that i have mamp on my system, and i was hoping to use its mysql. From what I’ve gathered I understand that its 32bit, so i have to import MySQLdb against a 64bit MySQL installation.
Any help would be sooooo much appreciated!
Splendorsine, dk, ash – Because things have changed so much between Leopard and Snow Leopard, please specify which OS you’re on – it matters a lot.
I haven’t personally encountered the “wrong architecture” problem so can’t really help with that one.
But I can say this: I upgraded to Snow Leopard on two machines. On one, I had previously existing bindings and libs installed, and things were a big pain. The other was a new machine and I was amazed to find that everything installed and compiled perfectly, with no issues whatsoever. This confirms my suspicion that most of these issues are caused by incompatibilities with previously installed libs and versions, and that your best bet is to back up and then wipe out lingering python libs from older Mac OS version and start fresh.
Not only that, but the MySQL site now offers an “official” python/mysql binding that worked for me on the new machine on the first try.
Ash, I’ll bet that applies to you. Trying to keep stuff from MAMP and combine it with new installs could be pretty tricky.
Splendorsine, dk, ash – I concur with Shacker on this. 2 Weeks ago, my co-Django-ist had on his MacBook Pro, 10.5.x and Py 2.4.x. He jumped through all the hoops for a week, MAMP, MacPorts, MySQL 32bit, then 64 bit … you get the gist.
We backed up, clean installed 10.6.0, upgraded with 10.6.4 combo and followed the instructions with the latest everything.
Smooth as butter.
MAMP s good for PHP/MySQL but for Django its best to install onto the Mac OS installs. Its a good exercise and it prepares you mentally for the next level.
And that level is the jump from the Development environment to the Production environment which includes virtual servers, “Nix” OSs, web servers, modules, etc.
Cheers guys, I was wondering the value of MAMP for learning django, as I’ve been using it to learn ruby on rails, but don’t actually do any PHP, so will try without it. I’ll keep you posted. I’ll definitely check out the MySQL site for that binding.
Thank Shacks and others. @spleach hit the nail on the head for me. I think I have a pretty clean system with standard python install (2.6 universal binary), Xcode installed and mysql.com distribution of MySQL 5.1. Yet I could not get a successful compile.
# file /usr/bin/python
/usr/bin/python: Mach-O universal binary with 3 architectures
/usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386): Mach-O executable i386
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc
When I tried to compile using the standard configs like
ARCHFLAGS='-arch x86_64' python setup.py buildI would get an error looking for Python.h.
pymemcompat.h:10:20: error: Python.h: No such file or directory
[ snip huge tail ]
So I modified the include paths for gcc using the CFLAGS env var. The complete recipe was:
export CFLAGS=”-I/Developer/SDKS/MacOSX10.6.sdk/usr/include -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6″
ARCHFLAGS=’-arch x86_64′ python setup.py build
ARCHFLAGS=’-arch x86_64′ python setup.py install
Mandric – You’re on Snow Leopard, right? And you realize that MySQL now provides an official python-mysql binding that should work out-of-the-box?
IOTW you shouldn’t need to be compiling this anymore – just get all of your manually installed libs out of the way and use the “official” dists – on Snow Leopard you should be good.
Yea I made a typo with what I wrote before, in one of the most critical yet subtle ways, but it looks like you figured it out. You shouldn’t need to add the python library to the CFLAGs environment variable. Python usually picks them up as is.
Anyway, original line I typo’d:-
$ export CFLAGS=”-L/usr/include/xtools”
The L is wrong. Should be:
export CFLAGS=”-I/usr/include/xtools” # or in your case:
Python should usually find everything it needs. I haven’t had problems with that. You might need to ensure that pymysqldb is import-able from python, and if not find it, and add the directory to the PYTHONPATH environment variable.. Worth a try perhaps. Good luck!
u’re a lifesaver. much thanks!
I get an error “bad value (apple) for -march= switch”. What does that mean? I’m running Snow Leopard w/ 32-bit mysql v5.5.
My terminal says when I build it:
copying MySQLdb/release.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb
building ‘_mysql’ extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -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.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.3-fat-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch i386
_mysql.c:1: error: bad value (apple) for -march= switch
error: command ‘gcc-4.0’ failed with exit status 1
Jim, are you really on OS X 10.3?
No, I’m running OSX 10.6.6 to be exact. Don’t know why the message says 10.3, but I got the problem solved. :) I needed MySQLdb to run on Eclipse. I had friend help me change the path to on run it and it works. Don’t have a logical reason why it works but it does. Appreciate the concern.
Ah – I had just noticed 10.3 strings in your output above. But glad you got it solved.
when i invent the next facebook, i’m coming back to pay you a million dollars for this. thank you sir.
I get this error any idea?
Referenced from: /Users/lucabertolotti/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
Reason: image not found
I have solved with :
Superb – hugely grateful for this; it heralded the end of a frustrating couple of hours. THANK YOU!
This works as of Jun 17 2011 gmt+8
I’m using osx10.6.7.
Had to install setuptools and mysql
Due to my previously using MAMP, I didn’t require mysql to be installed.
Required to export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/ as well to .profile
thank you so much after two days of headaches, the solution here solved my problem.
Your update for Snow Leopard finally helped me overcome my hurdles! Thank you for taking the time to share.
FYI… I’m using an install of mySQL packaged with XAMPP. So, my path to mysql_config in site.cfg is a little different (Applications/XAMPP/xamppfiles/bin/mysql_config). And I had to go back to XAMPP and install the development package to get the development header files.
Thank you for this tour-de-force of expository writing! I chickened-out on doing the install â€“ I’m using an SQL Batch file instead â€“ but if I ever get drunk and dangerous and try to install MySQLdb, this is where I’ll start :-)
“A shipwreck on the beach is a lighthouse to the sea” – Old Dutch Proverb
Wow. I am having a beer in your honor right now.
And then I’m having a beer to thank you for keeping me from smashing my computer.
Jim – Cheers on both counts!
great tutorial thanks! i’m able to successfully import MySQLdb but when i try to create a connection i get the following error:
OperationalError: (2002, “Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)”)
Daniel, have you verified that that’s the actual path to mysql.sock on your system? On my Mac it’s:
Im using mamp so the path should be :
how do i change the path in order to get it working with python? I tried
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
but it still won’t connect
thanks in advance
I’ve never tried MAMP (that approach has nothing to do with this tutorial), but you should be able to put that path in your Django settings.py, in the db section. You wouldn’t need a symlink then.
Hi, yes you are right sorry to be talking about MAMP, its a bit off topic but I was getting desperate for a solution. I somehow managed to to modify the connection to make it work for the location of the socket so thank you so much for your help!
This absolutely saved me, mainly the part about copying the macports build of mysqldb, I had been struggling with the i386 that pip was giving me.