Category: Python



I used Django 1.3.1, Python 2.7.1, virtualenv 1.6.4 and virtualenvwrapper 2.10.1 for the scenario described in this post.

After I’d deployed my first Django site that used virtualenv, I attempted to run sudo ./manage.py shell on the server to inspect some data on my site, but I got an error that django.core.management couldn’t be found. I figured I hadn’t set some path properly when I saw the error, but I was a little puzzled as to which, since my site worked perfectly in the browser, but failed to run Django management commands. I opened my project’s manage.py, and noticed the interpreter directive was just pointing at the system’s python. Since I was using virtualenv, I simply changed the Python in my manage.py to point at the one in my virtualenv and management commands worked awesomely again. See below:

Replace the first line with in manage.py with:
#!/usr/bin/env your-virtualenv-dir/your-virtual-env/bin/python


Update: 10/27/2011

I’m now on OS X Lion 10.7.2 and can confirm that the steps in this post will still get Python installed on your Mac.

Why Did I Replace the Default Python on Mac OS X?

Skip to the tutorial

Mainly, because tests were failing in my Django web application on my Mac that were passing on my Ubuntu setup. So I set out to create an up-to-date Python development environment on my Mac that works the same as the one on my Ubuntu box. Having successfully put Python 2.6.6 from python.org on my Mac, and fixed my web app, I’m sharing my results, setup, and solution with community.

Other Reasons to Replace the Python That Ships with Mac OS X

Python Interpreter Output: sys.maxint and sys.maxunicode

python -c "import sys; print sys.maxint;"
  • 9223372036854775807 (64-bit)
  • 2147483647 (32-bit)
python -c "import sys; print sys.maxunicode"
  • 1114111 (UCS-4)
  • 65535 (UCS-2)

Tutorial: How I Built Python 2.6.6 (64-bit) on Mac OS X

See also: Which Version of Python Do You Already Have Installed on Mac OS X? (Opens in a new Window)

  1. Install XCode from the Snow Leopard DVD
  2. Edit the first two lines of your .bash_profile to be:
    PATH="/usr/local/bin:/usr/local/sbin:/usr/local/bin/python:${PATH}"
    export PATH
  3. Download Python 2.6.6 source code:
    curl -O http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
    tar -xvf http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
    cd Python-2.6.6
  4. Edit /etc/paths and move the following line to the top of the file
    /usr/local/bin
  5. ./configure --disable-framework --disable-toolbox-glue \
    OPT="-fast -arch x86_64 -Wall -Wstrict-prototypes -fno-common -fPIC" \
    --enable-unicode=ucs4 LDFLAGS="-arch x86_64"
  6. make
  7. sudo make install
  8. cd into your home directory and type:
    source .bash_profile
  9. Open an interactive interpreter session, and if I’ve been successful in my instructions, you’ll see the following with different date/time information:
    Python 2.6.6 (r266:84292, Jan 30 2011, 21:45:16)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.

Which Version of Python Do You Already Have Installed on Mac OS X?

Open the Terminal.app and type which python,  and on a fresh install of Snow Leopard you should see:

/usr/bin/python

This is a symlink that points to your actual Python installation.

Next, type cd /usr/bin; ls -l python2.6, and the output should be something like:

lrwx-xr-x 1 root wheel 75 Oct 7 23:24 python2.6 ->
../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/Python2.6

Now open an interactive interpreter session:

Python 2.61 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>>