Install Python 2 and 3 on Mac OS X

October 24, 2014 — 2 Comments

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 python.org 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!

wget https://www.python.org/ftp/python/2.7.7/python-2.7.8-macosx10.6.dmg

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: http://goo.gl/9XRCF2

wget http://downloads.activestate.com/ActiveTcl/releases/8.6.1.0/ActiveTcl8.6.1.1.297588-macosx10.5-i386-x86_64-threaded.dmg

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:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

Install Pip

follow instructions here: https://pip.pypa.io/en/latest/installing.html

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

Install virtualenv

sudo pip install virtualenv

Close and reopen the terminal window and run:

which virtualenv

This should return:

/Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv

Install MacPorts

visit macports.org and follow most recent instructions:

wget https://distfiles.macports.org/MacPorts/MacPorts-2.3.0-10.9-Mavericks.pkg

double click .pkg file and follow the instructions.

Please read this if you have Homebrew on your Mac too: http://superuser.com/a/830796/248728

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

Note:

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 http://www.webapphero.org/configuring-bash-startup-files-on-os-x 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:

# https://virtualenv.pypa.io/en/latest/virtualenv.html#environment-variables-and-configuration-files
# 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: http://www.webapphero.org/configuring-bash-startup-files-on-os-x 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!)

Jay

Posts

2 responses to Install Python 2 and 3 on Mac OS X

  1. After accumulating too many version of python on my Yosemite Mac (system, macports 27 and 3, anaconda), I finally decided to corral them all with virtualenv. Your guide got me started easily.
    thanks

I welcome your comments