Setup Python and Django working environment in Mac OS X


Here is a quick way to setup python and django on the fly in Mac OS X Snow Leopard.

1. Python 2.7
It’s better to use stable / latest Python version for development. At this time, I use Python version 2.7.3 for development. You can grab this at http://www.python.org/download/releases/2.7.3/. Just click on .dmg files and install. Later you should do : “source ~/.bash_profile” to make this Python 2.7 as primary.

2. Install setuptools python 2.7
Download setuptools .egg files in http://pypi.python.org/pypi/setuptools/#credits and install by “sh setuptools-0.6c11-py2.7.egg” :

1
2
3
4
5
6
7
8
Processing setuptools-0.6c11-py2.7.egg
Copying setuptools-0.6c11-py2.7.egg to /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Adding setuptools 0.6c11 to easy-install.pth file
Installing easy_install script to /Library/Frameworks/Python.framework/Versions/2.7/bin
Installing easy_install-2.7 script to /Library/Frameworks/Python.framework/Versions/2.7/bin
Installed /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11

3. Install pip, virtualenv and virtualwrapper
Then we need to install this 3 python modules. Don’t worry, we no need “sudo” here because from now, all of this python modules installed inside “/Library/Frameworks/Python.framework/Versions/2.7/”.

1
2
3
easy_install pip
pip install virtualenv
pip install virtualenvwrapper

Edit your ~/.bash_profile for enabling virtualenvwrapper :

1
2
3
4
export WORKON_HOME=$HOME/.virtualenvs
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME # Tell pip to create its virtualenvs in $WORKON_HOME.
export PIP_RESPECT_VIRTUALENV=true # Tell pip to automatically use the currently active virtualenv.

Then you should load your ~/bash_profile by :

1
source ~/.bash_profile

Let’s play with virtualenv by creating a new one :

1
mkvirtualenv –no-site-packages –distribute trading

For leaving virtualenv environment, you can use “deactivate”. To change or go to virtualenv, use “workon” Or “workon” to see all virtualenv.

Go to Virtualenvwrapper folder by :

1
cd $WORKON_HOME

Let setup your environment default directory by :

1
2
3
cd $WORKON_HOME
cd /bin
vim postactivate

Now you should put your startup-folder, eg:

1
cd ~/htdocs/my-folder-here

We can change Terminal color virtualenvwrapper by edit $WORKON_HOME/postactivate :

1
2
3
#!/bin/bash
# This hook is run after every virtualenv is activated.
PS1="[e[34;1m](`basename "$VIRTUAL_ENV"`)[e[0m] $_OLD_VIRTUAL_PS1"

4. Install MySQL
Download MySQL 64bit for Mac OS X from mysql.com. After installation, you need to do symbolic link libmysqlclient to make it working with MySQL-Python module.

1
sudo ln -s /usr/local/mysql-5.5.23-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

It’s depend on your MySQL version.

5. Install homebrew
http://mxcl.github.com/homebrew/. After you install brew, then you can start installing git and other stuff like :

1
brew install git bash-completion wget

Load bash-completion to get ps1 in Terminal by add this into .bash_profile :

1
2
3
if [ -f `brew –prefix`/etc/bash_completion ]; then
. `brew –prefix`/etc/bash_completion
fi

This is my completed .bash_profile :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

#enables color in the terminal bash shell export
#sets up the color scheme for list export
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
alias ls=’ls -G’
alias ll=’ls -hl’

if [ -f `brew –prefix`/etc/bash_completion ]; then
. `brew –prefix`/etc/bash_completion
fi

export WORKON_HOME=$HOME/.virtualenvs
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME # Tell pip to create its virtualenvs in $WORKON_HOME.
export PIP_RESPECT_VIRTUALENV=true # Tell pip to automatically use the currently active virtualenv.
source /usr/local/etc/bash_completion.d/git-completion.bash
export PS1='[33[01;32m]u@h[33[00m]:[33[01;36m]w[33[00m] $(__git_ps1 "[[e[34;1m]%s[e[0m]]")$ ‘
export PATH=/usr/local/mysql-5.5.23-osx10.6-x86_64/bin:$PATH

6. Install django
Simple as “pip install django” 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.