Mac OSX — Installing a fresh Django environment with Homebrew, Git, and Virutualenv

How to install an isolated Django environment with Homebrew, Git, Virtualenv on OSX 10.6.5

I needed to start a local development environment, and every time I make a major switch whether it be a new server or a new OS, I try to make improvements based on what I’ve learned since the previous switch.

This time, I decided I need completely isolated virtualenvs, and I needed my code rewritten to use relative paths so that I can actually run my django app in my local environment without any changes.

Anyways…

First, install homebrew

I need the package manager Homebrew to do easy installs of various things like wget, Git, that are missing.

ruby -e "$(curl -fsSL https://gist.github.com/raw/323731/install_homebrew.rb)"

Next, install Xcode

You can find Xcode on the CD that ships with your mac.

Just open the CD, go to the Optional Installs folder, and click on the Xcode package.

This post has more detail if you require it http://www.mactricksandtips.com/2010/02/installing-xcode.html

Homebrew some Git

brew install git

Bam.

Use easy_install to grab virtualenv

easy_install virtualenv

Finally, time to set up our project!

First, we need to set up our project folder. I shall call it myproject. Very original.

mkdir myproject 
cd myproject

Now, it’s time to set up the virtual environment. I’m going ot use the –no-site-packages option to completely isolate this environment from the global python site-packages living in /Library/Python/X.X/site-packages

virtualenv venv --no-site-packages  # isolate from packages
cd venv  # cd into our newly created venv

virtualenv has set us up (the bomb) with a lib/ directory where python2.6 lives, and a bin/ directory where we have the activation scripts that force us to use this environment.

Since we want to install django into this virtualenv, run the bash script “bin/activate”

source bin/activate

Now we can install django. It’s now as easy as typing …

easy_install django

We can install most packages this way..

PS: I’d like to note that I had trouble installing PIL due to a bug. As of Nov 20, 2010, easy_install PIL will install to the correct directory but will not work. All you need to do is symlink the funky egg file to the same directory under the name PIL.

easy_install PIL
ln -s PIL............ PIL

Now we’re ready to start the django project.

cd ~/myproject
source venv/bin/activate # MAKE SURE you have activated your virtualenv, or else your python can't find the django you just installed.
django-admin.py startproject main

Congrats! Your isolated django install is ready on OSX

8 Comments

  1. You can use pip for python package management and virtualenvwrapper for more convenient workflow with virtualevnvs.

  2. Randy Smith says:

    Thank you very much for posting this. It saved me a lot of time. This was accurate, concise and best of all it works.

    Thanks

  3. neuronsong says:

    Thanks!

    Note:

    I didn’t have to do the “PIL” symlink. Perhaps they have fixed that bug?

    Also, I used this with my first django project and at first I was getting a slew of errors followed by:

    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

    Fix:

    easy_install MySQL-python

  4. Winston says:

    In which location did you mkdir myproject? In your web document root?

    1. Yuji says:

      It looks like for this example I used the home folder (~/), but it doesn’t matter where you put it. It shouldn’t be accessible by the web ; )

      On OSX, my typical setup is ~/Projects/myproject/

      In my production sites, I use/srv/myproject/.

      1. Winston says:

        thanks for your quick reply

  5. Alemosatei says:

    Hello. My favourite search mashine

    http://www.google.ru/images/srpr/logo11w.png

    OK google!
    OK google!
    OK google!

    Good-bye…
    Alezodasev

Leave a reply to Yuji Cancel reply