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.)
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
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
Update Active Tcl to fix IDLE bug: http://goo.gl/9XRCF2
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:
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:
follow instructions here: https://pip.pypa.io/en/latest/installing.html
sudo pip install virtualenv
Close and reopen the terminal window and run:
This should return:
visit macports.org and follow most recent instructions:
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
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:
# 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.)
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