Categorized: Apple, Django

Installing Django on OS X Leopard

If you haven’t already, stick your Leopard disk in and install Xcode 3.0.

Make a home for Django:

sudo mkdir /usr/local/django
cd /usr/local/django

Get the Django trunk from Subversion:

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

The Django trunk should now be in /usr/local/django/trunk/, and if you ever want to check out another branch, you have a nice spot to put it next to the trunk.


If you’ve previously install Python yourself, pay attention here

Find out if you’re using the right version of Python:

which python

If you don’t see:

/usr/bin/python

then delete whatever version you have sitting in your path, such as one in /usr/local/:

sudo rm /usr/local/bin/python

Once “which python” gives you “/usr/bin/python“, make sure your Python site-packages is in the right place. Running this command:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

should give you:

/Library/Python/2.5/site-packages

End of previous Python caveat


Now make sure Python knows where to find Django:

ln -s /usr/local/django/trunk/django /Library/Python/2.5/site-packages/django

Put the django-admin.py script on your system path:

sudo ln -s /usr/local/django/trunk/django/bin/django-admin.py /usr/local/bin/

Now you should have Django installed and ready to go. Run:

django-admin.py startproject project_name

and see.

If it works, cd into the folder it creates and try:

python manage.py runserver

Then go to localhost:8000 in your browser.

Django + MySQL:

If you want to use MySQL with Django, there’s a bit more to do. If you have MySQL ready to go, continue. Otherwise, go talk with Dan for a bit and come back here when you’re done.

Download the MySQLdb package. Stick in in /usr/local/src if you’re cool. Open it and edit site.cfg.

Change line ~ 13 from:

#mysql_config = /usr/local/bin/mysql_config

To:

mysql_config = /usr/local/mysql/bin/mysql_config

In _mysql.c:

Remove these lines (~ 37-39):

#ifndef uint 
#define uint unsigned int 
#endif

Go to the folder on the command line, then:

sudo python setup.py build
sudo python setup.py install

You might have to run the build command more than once. Don’t ask me why–I’m a copy and paster just like you.

That should do it. Try editing your settings.py file in your Django project and entering information for a MySQL database and see if it works.

If you experience something different than this or have any problems, please let me know directly or leave a comment.

Sources

Comments

JTJ → February 26th, 2008 at 9:05 am

Cool. I’ll use this when I eventually try out Django. One thing though: how did it get in /usr/local/src/django/trunk/ instead of /usr/local/django/trunk?

Trey → February 26th, 2008 at 9:38 am

That was a typo. Thanks for catching that. It’s fixed now.

Jason → April 10th, 2008 at 7:00 pm

When I try to execute django-admin.py startproject project_name i get a message reading: -bash: django-admin.py: command not found

I’m trying to get this running on a fresh install of Leopard. Any idea why it’s not working? I could really use some help.

Trey → April 10th, 2008 at 7:55 pm

@Jason - try this:

cd /usr/local/bin
ln -s /usr/local/django/trunk/django/bin/django-admin.py django-admin.py
Jason → April 11th, 2008 at 12:00 am

I’m getting: -bash: cd: /usr/local/bin: Not a directory

Trey → April 11th, 2008 at 9:07 am

You need to create that directory and put it on your system path. I refer you to this article.

Jason → April 11th, 2008 at 11:35 am

Got it. Thanks Trey!

Zach → April 14th, 2008 at 12:05 pm

after executing: ln -s /usr/local/django/trunk/django/bin/django-admin.py django-admin.py i get “file exists”

i then also get -bash: django-admin.py: command not found

after running django-admin.py startproject project_name .

Any ideas?

Trey → April 14th, 2008 at 1:08 pm

@Zach - What folder are you in when you are entering the ln -s command?

Zach → April 14th, 2008 at 1:49 pm

I had an unsuccessful install of Python before, i cleaned it up and got it working. Now i cant get the MySQL module to function… not even sure the errors i am getting yet. THANKS

stephhan → April 25th, 2008 at 9:47 am

i get sudo: svn: command not found

i am on 10.4 with xcode installed. thanks.

Jason Calleiro → May 13th, 2008 at 9:33 pm

Im going crazy here, maybe you can help. I installed django and everything through macports in the first tutorial i found. Later i realized that the version of python it installed was 2.4, i like the idea of being up to date with the latest. I followed your first couple commands and everything seems cool. I notice though that when i type python into the terminal it says “python 2.4.5″.

Then when i typed the next command:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

I get:

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages

Do you know how i can get it back to python 2.5 before moving forward with the rest of the installation tutorial.

Thanks to you or anyone that helps out in advance.

ps. I removed macports.

Phil → May 30th, 2008 at 4:17 am

Trey:

You might want to add a trailing slash to this command:

sudo ln -s /usr/local/django/trunk/django/bin/django-admin.py /usr/local/bin

As it won’t create the symlink properly - it’ll link django-admin.py to /usr/local/bin instead of /usr/local/bin/django-admin.py

Trey → July 15th, 2008 at 8:34 pm

Added the trailing slash. Thanks, Phil.

What do you think about that?

Elsewhere in the empire: Home, Blog, APOD