Morph AppSpace - Platform as a Service for RoR

How to install Django with MySQL on Mac OS X

Antonio Cangiano December 22nd, 2007

Installing Django on Mac OS X Leopard is supposed to be very straightforward, but if you are new to it, you may encounter a few puzzling questions and, in the case of MySQL, even a couple of headaches. I’m writing about this for the benefit of those of you who may attempt and struggle with this feat. MacPorts is not required for this how-to.

First and foremost, we are going to install Django from its svn repository, as opposed to obtaining the 0.96 release archive. The reason for this is that the trunk version implements a few new features. The development code is also rather stable and used by most people in production mode, even for sites like the Washington Post.

Checkout Django

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

Tell Python where Django is

Mac OS X 10.5 already ships with Python 2.5.1, thus you won’t have to install it. You can verify this by running python in the Terminal (use exit() to get out of the python shell). What you need to do is inform Python about the location of your django_trunk directory. To do this create the following file:

/Library/Python/2.5/site-packages/django.pth

Within this file, place only one line containing the path to your django_trunk folder. In my case, this is:

/Users/Antonio/Code/django_trunk

Of course, change it to the full path location of the directory on your filesystem.

Add django-admin.py to your PATH

The bin directory within the django folder (which is inside django_trunk itself) contains several management utilities. We need therefore to add the following to the PATH (again, change it to your own location):

/Users/Antonio/Code/django_trunk/django/bin

How you go about doing this, depends on the shell you are using, and I’m assuming you are able to export a shell variable on your own. In case you are using the bash shell (as I do) then you should have a .profile file in your home directory. Alternatively, you could just create a symlink to the utility django-admin.py in /usr/bin, but I recommend the former approach.

Grab and install MySQL

I would normally recommend PostgreSQL, at least until we have DB2 on Mac, but I realize that many of you use and prefer MySQL, which also seems to be the only one that requires special instructions due to a few installation issues when trying to get MySQL and Python to work together. You can install MySQL by grabbing and running one of the packages that are available on the official site. Choose the one for x86 and Mac OS X 10.4.

Install the MySQLdb driver

Get MySQL-python-1.2.2.tar.gz from SourceForge. Please follow these exact instructions because the source code won’t compile out of the box and will give you the following error when trying to build it:

/usr/include/sys/types.h:92: error: duplicate ‘unsigned’
/usr/include/sys/types.h:92: error: two or more data types
in declaration specifiers
error: Setup script exited with error: command ‘gcc’ failed

Run the following:

tar xvfz MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2

At this point, edit the _mysql.c file and comment out lines 37, 38 and 39 as follows:

//#ifndef uint
//#define uint unsigned int
//#endif

Now, from the MySQL-python-1.2.2 folder run:

python setup.py build
sudo python setup.py install

If you still get an error (and only in that case) you’ll need to edit the site.cfg file within the same folder and set threadsafe = False, before running the two commands above once again.
If instead, you don’t receive an error but you see warnings about files not required on this architecture, don’t be concerned about them. The last step required is to create a symbolic link with the following command:

sudo ln -s /usr/local/mysql/lib/ /usr/local/mysql/lib/mysql

All these adjustments are required because we are building and installing the driver on Mac and not on Linux.

Verify the installation

You should be all set now, but let’s verify this right away. Open Terminal and run the following commands in the python shell (start this with the python command).

Verify that MySQLdb is correctly installed:

>>> import MySQLdb
>>> MySQLdb.apilevel
‘2.0′

Now, verify that Django is working:

>>> import django
>>> print django.VERSION
(0, 97, ‘pre’)

By running exit() get out of the python shell, and verify that django-admin.py is in your path:

django-admin.py
Type ‘django-admin.py help’ for usage.

If you obtain a similar output for all three of them, you are really set to write the next YouTube.

Where to go from here

Now that Django is installed, you can go read the Django Book 1.0 that’s available for free online. Something equally well done and useful is really missing from the Rails community. Above all, experiment, Django (and programming in general) is learnt by doing. The Definitive Guide to Django: Web Development Done Right is also available for purchase in its deadtree version, which just came out. It’s cheap and it’s already a best seller on Amazon. Despite the availably of a free version online, I like having paper versions of tech books so that I can read without staring at the monitor. Furthermore, I feel like rewarding the authors (who are also the framework creators), while encouraging publishing companies that are willing to allow authors to make their books available for free on the web. Well done guys!


If you enjoyed this post, then make sure you subscribe to my RSS Feed.

24 Responses to “How to install Django with MySQL on Mac OS X”

  1. C8E ITALY Ubuntu Linux Mozilla Firefox 2.0.0.11 on 22 Dec 2007 at 6:17 pm

    Great guide, good work Antonio

  2. [...] stable and used by most people in production mode, even for sites like the Washington Post.Mas: http://antoniocangiano.com/2007/12/22/how-to-install-django-with-mysql-on-mac-os-x/ [...]

  3. [...] how-to is essentially the same as my previous one, only this time I’ve provided step-by-step instructions for installing Django with PostgreSQL [...]

  4. [...] How to install Django with MySQL on Mac OS X | Zen and the Art of Ruby Programming Covers a couple of problems in the installation of mysql-python (tags: django python mysql osx) [...]

  5. Michael UNITED STATES Mac OS X Safari 523.10.6 on 18 Jan 2008 at 6:52 pm

    I followed your instructions, but I get the following error why I try to run “python setup.py build” in the “MySQL-python-1.2.2″ folder:

    EnvironmentError: /usr/local/bin/mysql_config not found

    Any suggestions?

  6. Michael UNITED STATES Mac OS X Safari 523.10.6 on 18 Jan 2008 at 10:30 pm

    Ok, so I managed to solve the above error by entering the following command: “sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config”. However, when I type “import MySQLdb” in the python shell to confirm that it is working, I get another error: 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 6, in __bootstrap__
    ImportError: dlopen(/Users/michael/.python-eggs/MySQL_python-1.2.2-py2.5-macosx-10.3-fat.egg-tmp/_mysql.so, 2): Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
    Referenced from: /Users/michael/.python-eggs/MySQL_python-1.2.2-py2.5-macosx-10.3-fat.egg-tmp/_mysql.so
    Reason: image not found

  7. Michael UNITED STATES Mac OS X Safari 523.10.6 on 18 Jan 2008 at 10:42 pm

    The following command fixed my problem:

    sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

  8. k00k UNITED STATES Mac OS X Mozilla Firefox 2.0.0.11 on 27 Jan 2008 at 4:23 pm

    Antonio,

    Graci! You saved me with this one, I was going crazy. You fixed me.

  9. Brandon Taylor UNITED STATES Mac OS X Safari 523.12.2 on 02 Feb 2008 at 3:27 pm

    Thank you SO much for this article! I struggled for quite a while to get MySQL configured to use with Django until I found this article.

    Many, many, MANY thanks!

  10. mikeal UNITED STATES Mac OS X Mozilla Firefox 3.0b3pre on 05 Feb 2008 at 1:35 pm

    Thanks man, i could not find any way to get these stupid mysql bindings to compile until I read about commenting out those lines.

    You saved me man.

  11. [...] La seva instal·lació en un MacOS X (per variar) és una mica entrebancada, és a dir, no és difícil, però si susceptible de problemes. Al final ho he pogut instal·lar amb les intruccions que he trobat aqui. [...]

  12. Ryan Donohue UNITED STATES Mac OS X Mozilla Firefox 3.0b3 on 01 Mar 2008 at 4:39 am

    Hi,

    I was having a lot of trouble with a “mysql_config not found” error.

    So, for future beginners reading this, the following commands worked for me. Instead of

    python setup.py build
    sudo python setup.py install

    I used,

    env PATH=/usr/local/mysql-5.0.51a-osx10.4-i686/bin:$PATH python setup.py build

    and

    sudo env PATH=/usr/local/mysql-5.0.51a-osx10.4-i686/bin:$PATH python setup.py install

    Replace “mysql-5.0.51a-osx10.4-i686″ with whatever version you are using.

  13. Erik UNITED STATES Mac OS X Safari 523.15 on 06 Mar 2008 at 1:24 am

    Thanks! _mysql.c comment was really helpful.

  14. sofia PORTUGAL Mac OS X Mozilla Firefox 3.0b3 on 07 Mar 2008 at 9:35 pm

    Thanks!! This was really helpful.
    Just to help other people, i also had difficulty installing the mysqlbindings and this article helped http://www.keningle.com/?p=11 . Besides that i also had to add to setup_posix.py the following
    mysql_config.path = “/usr/local/mysql/bin/mysql_config” just before
    f = popen(”%s –%s” % (mysql_config.path, what)) .
    Finally django-admin.py was giving me trouble because i had added the path to .profile and not to .bash_profile .

  15. Brandon UNITED STATES Mac OS X Safari 525.13 on 23 Mar 2008 at 11:01 pm

    Any ideas where I went wrong? I am running Leopard and Python 2.5.1. I followed the directions above to the letter. Everything went fine until this build step for MySQL python.

    ::~/MySQL-python-1.2.2/> python setup.py build
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.3-fat-2.5/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.3-fat-2.5/_mysql.o -Os -arch x86_64 -fno-common
    In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:57,
    from pymemcompat.h:10,
    from _mysql.c:29:
    /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyport.h:734:2: error: #error “LONG_BIT definition appears wrong for platform (bad gcc/glibc config?).”
    In file included from _mysql.c:35:
    /usr/local/mysql/include/my_config.h:1032:1: warning: “SIZEOF_LONG” redefined
    In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:8,
    from pymemcompat.h:10,
    from _mysql.c:29:
    /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyconfig.h:807:1: warning: this is the location of the previous definition
    error: command ‘gcc’ failed with exit status 1

  16. Brandon UNITED STATES Mac OS X Safari 525.13 on 23 Mar 2008 at 11:52 pm

    Never mind my last question…I’m guilty of a n00b mistake - I installed the x86 64 bit version of MySQL and it was conflicting with the i386 version of Python during the build.

  17. Jason UNITED STATES Mac OS X Mozilla Firefox 2.0.0.8 on 02 May 2008 at 9:47 am

    Very helpful, thanks for getting me started with Django on Leopard!!!

  18. Sebastian GERMANY Mac OS X Safari 525.18 on 10 May 2008 at 8:34 am

    You just saved my life!! Thanks a lot!! Just want to mention here that I got an error on the first try because my MySQL is installed in a different directory than specified in site.cfg. No more problems after fixing that!

    Hey ho, let´s go!

  19. Joel UNITED KINGDOM Mac OS X Mozilla Firefox 2.0.0.14 on 10 May 2008 at 4:12 pm

    works for me.. been trying to find a fix for this for hours.. thank you so very much!

  20. Joel UNITED KINGDOM Mac OS X Mozilla Firefox 2.0.0.14 on 10 May 2008 at 4:13 pm

    Thank you so very much!

  21. ace place UNITED STATES Mac OS X Mozilla Firefox 2.0.0.14 on 20 May 2008 at 6:17 pm

    dood! you rock.

  22. Lindsay UNITED STATES Mac OS X Safari 525.18 on 02 Jun 2008 at 8:21 pm

    Just installed the MySQL db package using the instructions (after a fruitless attempt by myself). Thanks so much for providing us with this.

  23. [...] http://antoniocangiano.com/2007/12/22/how-to-install-django-with-mysql-on-mac-os-x/ [...]

  24. joel AUSTRALIA Mac OS X Safari 523.15 on 11 Jun 2008 at 12:18 am

    Hi,

    I am still bothered by the error hereafter, hope you can help. Many thanks for sharing all this.

    joel

    Python 2.5.1 (r251:54863, Feb 4 2008, 21:48:13)
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import MySQLdb
    Traceback (most recent call last):
    File “”, line 1, in
    File “build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py”, line 19, in
    File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 7, in
    File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 6, in __bootstrap__
    ImportError: dynamic module does not define init function (init_mysql)
    >>>

Trackback URI | Comments RSS

Leave a Reply