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!

Related Articles:


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

Comments

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

  1. C8E on December 22nd, 2007 6:17 pm

    Great guide, good work Antonio

  2. DbRunas - How to install Django with MySQL on Mac OS X on December 22nd, 2007 9:34 pm

    [...] 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. Installing Django with PostgreSQL on Ubuntu | Zen and the Art of Ruby Programming on December 26th, 2007 4:45 am

    [...] 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. Craig Vidler | Weblog » links for 2008-01-06 on January 6th, 2008 2:18 pm

    [...] 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 on January 18th, 2008 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 on January 18th, 2008 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 on January 18th, 2008 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 on January 27th, 2008 4:23 pm

    Antonio,

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

  9. Brandon Taylor on February 2nd, 2008 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 on February 5th, 2008 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. Pícsels, bytes i alguna cosa més … » Blog Archive » Python, MySQLdb i Mac OS X on February 7th, 2008 1:07 pm

    [...] 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 on March 1st, 2008 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 on March 6th, 2008 1:24 am

    Thanks! _mysql.c comment was really helpful.

  14. sofia on March 7th, 2008 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 on March 23rd, 2008 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 on March 23rd, 2008 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 on May 2nd, 2008 9:47 am

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

  18. Sebastian on May 10th, 2008 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 on May 10th, 2008 4:12 pm

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

  20. Joel on May 10th, 2008 4:13 pm

    Thank you so very much!

  21. ace place on May 20th, 2008 6:17 pm

    dood! you rock.

  22. Lindsay on June 2nd, 2008 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. mechanical girl » Blog Archive » Installing Django with MySQL on Mac OS X on June 5th, 2008 12:49 am
  24. joel on June 11th, 2008 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)
    >>>

  25. Kevin M on August 19th, 2008 11:01 pm

    I’m getting the following error (btw, I’m using MAMP, could that be the problem?)

    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 -arch ppc -arch i386 -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/Applications/MAMP/Library/include/mysql -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
    unable to execute gcc: No such file or directory
    error: command ‘gcc’ failed with exit status 1

  26. Jason on September 19th, 2008 2:27 pm

    Hey guys, just followed this guide and installed django, mysql with mamp setup. Both mysql and django are checked under python and seem to work. But once i start to compile my project it gives me this list of errors:
    Unhandled exception in thread started by
    Traceback (most recent call last):
    File “/Library/Python/2.5/site-packages/django/core/management/commands/runserver.py”, line 48, in inner_run
    self.validate(display_num_errors=True)
    File “/Library/Python/2.5/site-packages/django/core/management/base.py”, line 122, in validate
    num_errors = get_validation_errors(s, app)
    File “/Library/Python/2.5/site-packages/django/core/management/validation.py”, line 65, in get_validation_errors
    connection.validation.validate_field(e, opts, f)
    File “/Library/Python/2.5/site-packages/django/db/backends/mysql/validation.py”, line 8, in validate_field
    db_version = connection.get_server_version()
    File “/Library/Python/2.5/site-packages/django/db/backends/mysql/base.py”, line 275, in get_server_version
    self.cursor()
    File “/Library/Python/2.5/site-packages/django/db/backends/__init__.py”, line 56, in cursor
    cursor = self._cursor(settings)
    File “/Library/Python/2.5/site-packages/django/db/backends/mysql/base.py”, line 262, in _cursor
    self.connection = Database.connect(**kwargs)
    File “/Users/Duo/svn/cse360/flix/__init__.py”, line 74, in Connect

    File “/Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/MySQLdb/connections.py”, line 170, in __init__
    _mysql_exceptions.OperationalError: (2003, “Can’t connect to MySQL server on ‘devour.ath.cx’ (60)”)

    Anyone got any idea? Tried search around google but didn’t find much info. Any help would be appreciated!

  27. django on leopard | alkalias on October 10th, 2008 3:57 am

    [...] think thats it … short and sweet … here are the references I used: How to install Django with MySQL on Mac OS X Notes: installing Django / Python / Mysql on OS X ………Tags » django [...]

  28. luminescente » Django on November 10th, 2008 8:18 pm

    [...] algumas compliações a instalar o Django com suporte a MySQL no OS X, mas este post [...]

  29. frink on December 8th, 2008 1:36 pm

    Thanks. Many, many sites have similar fixes for the Python MySQLdb fix, but only yours included the ‘//’, which reminded me that the _mysql.c file required ‘//’ for a comment, and not just the ‘#’ symbol. You limited my headbanging to hours instead of days. Thank you, thank you, thank you.

  30. Sami on December 29th, 2008 6:14 pm

    What are the disadvantages of installing Mysql with Mamp, not similarly as Antonio? This would make the process of installing Mysql much straightforward, at least for offline testing.

  31. Nikita on January 3rd, 2009 11:56 am
    Build _mysql on MacOSX Leopard with MacPorts 1.700.
    
    0.
    install macports (http://macports.org) packages 'mysql5', 'python26', 'python_select'
    > sudo /opt/local/bin/port -v install mysql5
    > sudo /opt/local/bin/port -v install python26
    > sudo /opt/local/bin/port -v install python_select
    
    activate python2.6 from macports using python_select
    > python_select python26
    
    1. install new setup tools
    Download 'setuptools-0.6c9-py2.6.egg' from http://pypi.python.org/pypi/setuptools/, then
    > sh setuptools-0.6c9-py2.6.egg
    
    2. (*optional) add symlink
    sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/easy_install-2.6 /opt/local/bin/easy_install-2.6
    
    3. download MySQL-python-1.2.2.tar.gz and extends
    > tar xvfz MySQL-python-1.2.2.tar.gz
    
    4.
    > cd MySQL-python-1.2.2
    
    5. edit the _mysql.c file and comment out lines 37, 38 and 39:
    ===
    //#ifndef uint
    //#define uint unsigned int
    //#endif
    ===
    
    6. edit site.cfg: uncomment and change mysql_config to:
    ===
    mysql_config = /opt/local/bin/mysql_config5
    ===
    
    7. build
    > python setup.py build
    > sudo python setup.py install
    
  32. scrapmaster on January 4th, 2009 7:52 pm

    Thank you, sir, thank you, thank you. Sometimes I just love the internet.

  33. How to finally fix that damned MySQLdb module on Leopard « Nerd Stuff on January 18th, 2009 5:50 am

    [...] Antonio Cangiano – How to install Django with MySQLdb on MacOSx [...]

  34. Programando em Django no seu Sistema Operacional favorito | Profissionais TI on February 23rd, 2009 11:56 am

    [...] Zen and the Art of Programming – How to install Django with MySQL on Mac OS X: http://antoniocangiano.com/2007/12/22/how-to-install-django-with-mysql-on-mac-os-x/ [...]

  35. Carl G on March 9th, 2009 12:59 pm

    Thanks!

  36. mika on April 4th, 2009 7:29 am

    NOT funny: you have to install dev tools!! So going django with mySQL requires dev tools???? What is this?? AND – WHY is there just not an gui installer that does all the above crap?

  37. Daniel S on June 18th, 2009 8:11 am

    There is a Django one click installer available!
    Check out Bitnami.org (only django + MySQL at the moment, but PostgreSQL is in the works to)

  38. Andy on February 21st, 2010 1:52 pm

    If you’re having trouble getting it installed the new version of mysql-python requires some different instructions, also if you’re using 64 or 32 bit versions of python or mysql will make a difference. There’s some information here:

    http://learninglamp.wordpress.com/2010/02/21/mysqldb-python-mysql-and-os-x-a-match-made-in-satans-bum/

Leave a Reply