Sunday, December 29, 2013

Django on Mac OS X (Django 1.5.5., Postgres)

My first tryst with Web Programming was in 1999. When I was still in college, and was an intern in Philips Software Center, Bangalore.

One of the teams there wanted an online testing software and my internship was no way related to programming. (Was doing some CMM Gap Analysis stuff!) - I decided to do this as a side project.

I had NO idea what it would involve. I then came across JWSDK Java Web Service Development Kit 1.0. That was the day it was launched. I just downloaded that, Java and got cracking. I learnt about GET/POST methods, and was my first tryst with Servlets. Even learnt the concept of cookies from first principles ;-) - It was a success, in the sense that people could use it for a limited time and it worked!

From the heady world of internet, I plunged into the absolute basics for many years - Embedded Realtime Systems Programming in the domain of Wireless / Networks became my area of focus. However, I never lost touch of my first love - Web App development and AI.

I thought I should restart by learning some of the current web frameworks. My options were clear and out in open - Ruby on Rails vs Django. I decided to take the plunge to Rails a few weeks ago. However, I had so many issues - Don't know Ruby, Rails, Web Frameworks etc. When you try a new problem, it is absolutely imperative to reduce the number of unknowns. After a few weeks, I decided to go Django. I am relatively good at Python. Now, I had to learn only the web framework (no need to learn Ruby or the DSL (rails))

---

I decided to get started on my Mac (MBP RD 2012) with OS X Mavericks. Little did I know that I would take a day to get this started!

The best way to get started with Django is https://docs.djangoproject.com/en/1.5/intro/tutorial01/

I had Python 2.7.5 installed (2.7.6 is recommended for Mac OS X Mavericks)

Installing Django was a piece of cake with
pip install django==1.5.5

I had to do that because the tutorial assumes 1.5.5. and by not specifying the version, I got 1.6 (the latest stable release). And, I know that these are two MAJOR releases (1.5 vs 1.6)

I also had the hello world project executed. Now came the tough part. I had decided to use Postgres as my backend (know someone who is really good, and wanted to do a quick prototype of a concept with him)

I downloaded and installed the Graphical installer for Mac OS X (9.3.2) - http://www.postgresql.org/download/macosx/

Then I had to install psycopg2 (python binding for postgres) - All hell broke loose

1. sudo pip install failed 
 Because the compiler couldn't find <stdio.h>

Solution: Download and install Command Line Tools for Mac OS X Mavericks
https://developer.apple.com/downloads/index.action
Command Line Tools (OS X Mavericks) for XCode - Late October

2. After this, psycopg2 got going with a LONG loop (nearly endless) - It gave me some error involving XCode. I thought XCode was not required to get this up and going. I had to download the 2.04 GB Xcode 5.02 (Damn!) - Thankfully, I had my Airtel 4G dongle (connected to a HSPA+ network in Chennai) and hence got this up in an hour and a half. Should be faster in an LTE connection in Bangalore.

And then I could compile and build psycopg2

3. My happiness was shortlived. I opened a Python prompt and entered import psycopg2

from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID ImportError: dlopen(/Users/sviyer/dev/.virtualenvs/dbtest/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0 dylib Referenced from: /Users/sviyer/dev/.virtualenvs/dbtest/lib/python2.7/site-packages/psycopg2/_psycopg.so Reason: image not found 


There was a workaround - The issue was python runtime was not able to locate the libssl in standard location - The solution:

export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.1c/lib


Now when I import psycopg2, it worked! 

But, that's not end of the story

4. I tried setting this environment variable in /etc/launchd.conf - Unfortunately, this did not work
Srirams-MacBook-Pro:etc sriram$ more /etc/launchd.conf 
DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.3/lib


5. Finally, thank stackoverflow :), I created symlinks for the same in /usr/lib (http://stackoverflow.com/questions/16407995/psycopg2-image-not-found)
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libssl.1.0.0.dylib /usr/lib
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libcrypto.1.0.0.dylib /usr/lib

I did write that we shouldn't use stackoverflow as primary debug tool (http://yourstory.com/2013/12/death-computer-science/) - However, In my defence, I did use it after my digging around! (Though shalt not smite me)

In case you are one of the newbies like me trying to setup Django on your Mac OS  X, hope this helps.

In summary:

1. Install Command Line Tools for X Code
2. Install X Code
3. Install Django 
4. Install Postgres
4. Install psycopg2

Get going! 

Will share my adventures (if it would be useful to a larger community)

Have fun!