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 https://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!
Get more stuff like this
Subscribe to my mailing list to receive similar updates about programming.
Thank you for subscribing. Please check your email to confirm your subscription.
Something went wrong.
Great guide, good work Antonio
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?
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
The following command fixed my problem:
sudo ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
Antonio,
Graci! You saved me with this one, I was going crazy. You fixed me.
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!
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.
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.
Thanks! _mysql.c comment was really helpful.
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 .
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
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.
Hi Brandon! I am getting the same error. ‘gcc failed with exit status’. I am using XAMPP on my Mac OSX. Please let me know how did you solve the problem?
Thanks,
Sushil
email: sushtaurus@gmail.com
Hey i got it working!
Changed the Python in to 32 bit version:
defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
defaults write com.apple.versioner.python Prefer-32-Bit -bool no
Installed developer package for XAMPP.
Very helpful, thanks for getting me started with Django on Leopard!!!
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!
works for me.. been trying to find a fix for this for hours.. thank you so very much!
Thank you so very much!
dood! you rock.
Just installed the MySQL db package using the instructions (after a fruitless attempt by myself). Thanks so much for providing us with this.
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)
>>>
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
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!
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.
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.
Thank you, sir, thank you, thank you. Sometimes I just love the internet.
Thanks!
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?
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)
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/