Install of Python dev environment on OSX

After much trial and error, this is what worked for me on a macbook pro, running snow leopard 10.6.6.

$ brew install python
$ sudo easy_install pip
$ pip install virtualenv
$ pip install virtualenvwrapper 
$ vi ~/.profile
   # add this line to .profile
   source /usr/local/Cellar/python/2.7.1/bin/virtualenvwrapper.sh

$ source ~/.profile

First install a current Python (OSX python is outdated).

Then use easy_install to install its replacement: pip. (use pip after this)

Then use pip to install virtualenv and virtualenvwrapper (python's env manager.  Think Ruby rvm)

The docs for virtualenvwrapper are at http://www.doughellmann.com/docs/virtualenvwrapper/

Example of creating python env

$ mkvirtualenv env-0
New python executable in env-0/bin/python
Installing setuptools............................done.
virtualenvwrapper.user_scripts creating /Users/sam/.virtualenvs/env-0/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/sam/.virtualenvs/env-0/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/sam/.virtualenvs/env-0/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/sam/.virtualenvs/env-0/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/sam/.virtualenvs/env-0/bin/get_env_details

$ ll $WORKON_HOME
total 88
drwxr-xr-x  5 sam  staff   170 Mar 25 08:22 env-0
-rwxrwxr-x  1 sam  staff   106 Mar 25 08:18 get_env_details
-rw-r--r--  1 sam  staff  4082 Mar 25 08:33 hook.log
-rwxrwxr-x  1 sam  staff    92 Mar 25 08:18 initialize
-rwxrwxr-x  1 sam  staff    69 Mar 25 08:18 postactivate
-rwxrwxr-x  1 sam  staff    71 Mar 25 08:18 postdeactivate
-rwxrwxr-x  1 sam  staff    69 Mar 25 08:18 postmkvirtualenv
-rwxrwxr-x  1 sam  staff    63 Mar 25 08:18 postrmvirtualenv
-rwxrwxr-x  1 sam  staff    70 Mar 25 08:18 preactivate
-rwxrwxr-x  1 sam  staff    72 Mar 25 08:18 predeactivate
-rwxrwxr-x  1 sam  staff    94 Mar 25 08:18 premkvirtualenv
-rwxrwxr-x  1 sam  staff    64 Mar 25 08:18 prermvirtualenv

Getting MySQLdb For Python Working on Snow Leopard

I'm just getting into Python and Django programming so I've been tinkering with the tutorials online to get familiar with it.  I develop on a Macbook running 10.6.x and the pre-installed Python was working just fine for me until I needed to connect to MySQL for some of the Django work.  To do this you need MySQLdb for python installed.  I'm new to setting up Python environments and I'm sure there is a way to get Apple's Python working with MySQL but it just was not working for me, so after many, many failed attempts, I gave up and started developing Python on an Ubuntu VM on the Macbook (gotta love apt-get). Some co-workers at Mozilla then turned me onto a new(ish) package manager for OSX called HomeBrew.  Figuring that Apple's 'special' python was the root of my problems above (I've been down that road with apache and PHP), I thought I'd replace Apple's python with one installed using Homebrew and attempt the MySQLdb install again. Here is a summary of that process. Install Homebrew:
$ curl http://gist.github.com/raw/323731/HEAD/install_homebrew.rb -o /tmp/install_homebrew.rb
Recursively change the ownership of /usr/local to your user (not the most secure thing to do but I am comfortable with it on my dev laptop.  Keeps you from running brew as root)
$ sudo chown -R $USER /usr/local
Run the installer:
$ ruby /tmp/install_homebrew.rb
Adjust your PATH to get /usr/local/bin at start of path so brew installed is picked up first I did this by adding the line below to ~/.profile
export PATH="/usr/local/bin:$PATH"
Now you can start installing with brew:
$ brew install python
You will need setuptools to install MySQLdb
$ brew install setuptools
Install 64bit Mysql.  Folks accomplish this in many different ways, from src, binaries, I believe you can event brew install it.  I originally had the 32-bit binary installed (from http://dev.mysql.com/downloads/mysql/) and was unable to get MySQLdb working with that.  So I just downloaded and ran the install for the 64bit version binary. Now on to MySQLdb for Python,  download it at http://sourceforge.net/projects/mysql-python/ Follow the install in the README file which should be something like this:
$ tar xfz MySQL-python-1.2.1.tar.gz
 $ cd MySQL-python-1.2.1
 $ # edit site.cfg if necessary
 $ python setup.py build
 $ sudo python setup.py install # or su first
Then test
$ cd
$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 12:36:14)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
As is often the case in Linux/Unix the 'no output' you get after import MySQLdb is the good news you were looking for.  You are most likely all set to use Python and MySQL together now.
Tagged Python