Archives For October 2014

By default, OS X comes with a buggy implementation of bash startup files. Here’s my approach to configuring bash’s startup files:

Feel free to follow the steps below or enjoy this 12 minute video discussion:

Create .bash_profile in $HOME if .bash_profile doesn’t already exist and set its file permissions:

touch .bash_profile
chmod 755 .bash_profile

Edit .bash_profile to include any “bash specific” commands which will only work in bash, and not in other POSIX shells (zsh, etc.)

Create .profile in $HOME if .profile does exist and set its files permssions:

touch .profile
chmod 755 .profile

Call .profile from the end of .bash_profile by adding the following:

if [ -f ~/.profile ]; then 
        source ~/.profile 

Utilize .profile to include all modifications of PATH or any environment variables.

Create .bashrc in $HOME if .bashrc doesn’t exist and set its file permissions:

touch .bashrc
chmod 755 .bashrc

Add the following lines to .bashrc:

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        source /etc/bashrc

Call .bashrc from the end of .profile by adding the following lines to .profile:

if [ -f ~/.bashrc ]; then 
        source ~/.bashrc 

Use .bashrc for creating aliases and other functions which won’t pollute the the environment variable namespace with duplicate names etc. For good measure, include the following shell function, written by Gordon Davisson:

pathadd() {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then

Use the following syntax when adding a directory, such /usr/bin to the PATH within .profile:

pathadd /usr/bin

Hopefully this configuration setup will promote good organization and express intentions clearly. Let me know if you come up with something better.

My best,


I created the following post to document the steps I took to update the existing installation of Python on my mac, as well as setting up additional installations of various versions of Python to be used as templates for fresh virtual python environments.

This document claims no exercise of best practice, simply an approach that seems to be working for me after several months of use. Your mileage may vary, but I hope you learn something valuable for your own journey. If you use Homebrew, it will issue a warning when detecting this Python configuration, so again only use this setup if you are comfortable with that.

On a more personal note, I can tell you beginners out there that setting up your system to your liking can be a painful process, especially the first couple years. It does get easier and no, your not stupid, this stuff is just layered with years of historical baggage and it just takes time to get comfortable with it. If I had to recommend one book to help, it would be A Practical Guide to Linux, Commands, Editors, and Shell Programming by Mark G. Sobell, which also covers OS X.

Goal: Install Python 2 and 3 on Mac OS X. To keep these separate from the system Python version, so they can be used as clean prototypes for virtual environments.

Motivation: As a Python developer, I want to consciously control the use Python on my system (OS X Mavericks 10.9.2)

Note: Many steps are performed on the command line to facilitate learning and scriptability but feel free to browse and click where you prefer. (For example, instead of using wget in the 2nd step, you can just browse the site and locate the most recent OS X python dmg file for download.)

Game Plan

Update the built-in ‘system’ version of Python 2 with a clean install. Use this system version to install pip. Then pip install virtualenv.

Install 2 more instances of Python (the most recent versions from each Python 2 and Python 3) in a non-system location.

Point virtualenv at the non-system Python interpeters for creating new Python development environments. These environments can be upgraded independently of the system’s Python, increasing the flexibility of the system’s Python configuration.

Change into home directory

cd ~

Download most recent version of Python 2 for use by ‘system’

UPDATE 2015/01/17:  Oops! wget isn’t available on vanilla mac systems. Jump ahead to the “Install Macports” section, follow it, then run “sudo port install -t wget” from your command line, then come back to this point and proceed. Whew!


Note: OS X doesn’t come with wget installed, so use curl instead of wget in that case.

Mount the disk image file

hdiutil mount python-2.7.7-macosx10.6.dmg

Install the Meta Package file (the Python.mpkg file found within the newly mounted image)

sudo installer -pkg /Volumes/Python\ 2.7.8/Python.mpkg/ -target /

Unmount image to tidy up

hdiutil unmount /Volumes/Python\ 2.7.8/Python.mpkg/


Confirm current path search to system python

Open a new Terminal window and then:

which python #returns "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"

Check your .profile (or equivalent) to ensure the installer appended the following text to update the PATH evnvironment variable:

# The original version is saved in .profile.pysave

 export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"

Update Active Tcl to fix IDLE bug:


double click this file to install

Set PYTHONPATH for GUI programs such as Finder

Xcode makes working with plist easy, so I recommend installing it.

If it does not exist:

mkdir ~/.MacOSX

Use XCode to create a plist file by selecting File | New | File, then select Resource (Under OS X) and then Property List

Then create a key called PYTHONPATH and set its value to:


Install Pip

follow instructions here:


Install virtualenv

sudo pip install virtualenv

Close and reopen the terminal window and run:

which virtualenv

This should return:


Install MacPorts

visit and follow most recent instructions:


double click .pkg file and follow the instructions.

Please read this if you have Homebrew on your Mac too:

It basically says to always use:

port install -t <portname>

when using Macports in order to avoid conflicts with Homebrew.

Install ‘segregated’ version of Python 2.7.8

sudo port -v -t install python27
cd /opt/local/bin


ls -lha | grep python

returns symlinks and source locations of installed python executables etc.:

lrwxr-xr-x 1 root admin 73B Jun 2 04:42 python2.7 -> /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

lrwxr-xr-x 1 root admin 80B Jun 2 04:42 python2.7-config -> /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config

lrwxr-xr-x 1 root admin 74B Jun 2 04:42 pythonw2.7 -> /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

Install ‘segregated’ version of Python 3.4

sudo port -v -t install python34

Add some aliases to ~/.profile ~/.bashrc to streamline creating new virtual environments

(UPDATE 2015/01/26: Please see for more information on why .bashrc is now my preferred location for storing bash aliases.)

The following step will cause python 3.4.1 to be used when creating new virtural environments with the arbitrarily chosen mkenv alias. This author uses Heroku as a deployment platform, which officially supports python 3.4.1 and 2.7.8 at this time.
Append the following to ~/.profile or ~/.bashrc depending on which you happen to use:

# Use /opt/local/bin/pythonX.X here because pointing to Python.framework causes permissions error

(UPDATE: 2015/01/26: Also, per my own advice found here: I recommend doing exports in ~/.profile, so the the following export should be done there.)

export VIRTUALENV_PYTHON=/opt/local/bin/python3.2

alias mkenv=’virtualenv’ #python 3.4.1
alias mkenv33=’virtualenv –python=/opt/local/bin/python3.3′
alias mkenv2=’virtualenv –python=/opt/local/bin/python2.7′ #python 2.7.8


The end. (Try installing ipython notebook next and enjoy coding with python!)