Python-MySQL Connections on Mac OS

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.

Python’s 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).

Solution:

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:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

Then try importing MySQLdb again. If it works, add that line to your .profile.

Good hunting.

More info

97 Replies to “Python-MySQL Connections on Mac OS”

  1. 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.

    Thanks again!

  2. 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…

  3. 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.

  4. Thanks so much…process was very smooth. This was a real timesaver for me….no more headbanging.

  5. 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.

  6. 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.

  7. 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)”)

    Any suggestions?

  8. 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”).
    Any ideas?

  9. 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…

  10. After looking at _mysql.c, I used the following with success:

    conn=MySQLdb.connect(host=’localhost’, user=’whatever’,
    passwd=’whatever’, unix_socket=’/tmp/mysql.sock’)

    So, thanks much again.

  11. You are an absolute lifesaver. I spent several hours doing everything I thought I could do… and finally your solution worked.

    THANK YOU.

  12. 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:
    cd /usr/local/mysql/lib
    file *

    If you see any libraries listed as “Mach-O 64-bit dynamically linked shared library x86_64” then you have the 64 bit version.

  13. 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.

  14. Scot,
    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).

  15. 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.
    Processing .
    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

    Installed /Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg
    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.

  16. Here’s another solution from the SourceForge page for the project:

    export ARCHFLAGS=’-arch i386 -arch x86_64′
    cd MySQL-python-1.2.3c1
    python setup.py build
    sudo python setup.py install

    Seems to work fine.

  17. Whoa.

    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
    import _mysql
    ImportError: dlopen(./_mysql.so, 2): no suitable image found. Did find:
    ./_mysql.so: mach-o, but wrong architecture
    >>>

    Any ideas?

  18. 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?

  19. 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.

  20. I followed the instructions to the letter…I even had to muck about with the stdarg.h file per this link:

    http://packetcloud.net/tag/mysql/

    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
    import _mysql
    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.

  21. 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:

    http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg

    and got:

    Mac OS X 10.5 (x86_64)

    and ran:

    mysql-5.1.39-osx10.5-x86_64.pkg

    once it was installed. I then ran:

    $ sudo ./MySQLCOM start
    Starting MySQL database server

    but then get:

    $ mysql
    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
    Password:
    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?

  22. 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:

    mysql-5.1.39-osx10.5

    but you’re talking about installing on Snow Leopard.

  23. You rock man! Thanks so much!
    I’ve been trying many times to get it working and you just explained it great.

    Thanks!
    Mariano.

  24. 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.

  25. 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

    export PYTHONPATH=”/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/”

    Good post!. I think you can add this as an alternative way :).

    L.

  26. 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“. :>)

  27. 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!

  28. DATABASE_HOST = ‘/tmp/mysql.sock’

    Thanks so much for this. After much frustration, this was able to get everything working.

  29. Incredible that this is still such a clusterf**k after so long. Do most python/django developers use Postgres, or something?

  30. 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.”

  31. 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!

  32. Hi,

    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:
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.5-fat64-2.6/MySQLdb
    running build_ext
    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
    Undefined symbols:
    “_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 :-)

  33. 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

  34. 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?

  35. 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…

  36. If you type:

    $ which python

    what do you get? I get:

    /usr/bin/python

    And if you run:

    /usr/bin/python

    does it then show 2.6.1?

  37. mmm if i type which python i get:
    /sw/bin/python
    and there is nothing in the folder usr/bin/python

  38. 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).

  39. Hi Shacker,

    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:
    I’m running:
    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    on
    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:
    “running build
    running build_py
    creating build
    creating build/lib.macosx-10.6-universal-2.6
    copying _mysql_exceptions.py -> build/lib.macosx-10.6-universal-2.6
    creating build/lib.macosx-10.6-universal-2.6/MySQLdb
    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
    creating build/lib.macosx-10.6-universal-2.6/MySQLdb/constants
    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
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.macosx-10.6-universal-2.6
    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?

  40. 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.

  41. 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.

  42. 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:-
    export LDFLAGS="-L/Developer/SDKs/MacOSX10.6.sdk/usr/lib"
    export CFLAGS="-I/Developer/SDKS/MacOSX10.6.sdk/usr/include"

    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.
    i.e.

    $ 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 ;-)

  43. 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
    execute_manager(settings)
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 438, in execute_manager
    utility.execute()
    File “/Library/Python/2.6/site-packages/django/core/management/__init__.py”, line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    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
    __import__(name)
    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
    __import__(name)
    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?

  44. 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.

  45. 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:
    shacker says:
    March 19, 2010 at 10:14 pm

    I get
    /usr/bin/python
    and
    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!

  46. 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.

  47. 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.

    Happy Django-ing

  48. 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.

  49. 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 build I 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 LDFLAGS=”-L/Developer/SDKs/MacOSX10.6.sdk/usr/lib”
    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″
    export PATH=$PATH:/usr/local/mysql/bin:/Developer/usr/bin
    ARCHFLAGS=’-arch x86_64′ python setup.py build
    ARCHFLAGS=’-arch x86_64′ python setup.py install

  50. 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.

  51. @Milan
    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:
    export CFLAGS=”-I/Developer/SDKS/MacOSX10.6.sdk/usr/include”

    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!

  52. 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:
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.3-fat-2.6/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.macosx-10.3-fat-2.6
    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

  53. 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.

  54. when i invent the next facebook, i’m coming back to pay you a million dollars for this. thank you sir.

  55. 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

    Thanks Luca

  56. Superb – hugely grateful for this; it heralded the end of a frustrating couple of hours. THANK YOU!

  57. 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

  58. 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.

  59. 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

  60. 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.
    Thank you.

  61. 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)”)

    any ideas?

    thanks

  62. Daniel, have you verified that that’s the actual path to mysql.sock on your system? On my Mac it’s:

    ‘HOST’: ‘/tmp/mysql.sock’,

  63. Im using mamp so the path should be :

    /Applications/MAMP/tmp/mysql/mysql.sock

    right?

    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

  64. 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.

  65. 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!

  66. 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.

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *