A virtualized Python setup on OS X (Mountain) Lion
Written by Arvinder Singh / July 06, 2012 / 4 mins read / Filed under Python, / Osx, / Numpy, / Scipy, / Matlibplot, / Virtualenv
Update 2012-07-26: Tested the install successfully for Mountain Lion.
Yesterday I replaced my slow old hard drive with a shiny new SSD drive. In the process I decided to reinstall Python on my machine and document the process for future reference.
I have a strong preference for command line installer such as homebrew
, versioning system git
and isolating installations into virtual environments. Come follow along.
Xcode
Start by installing Xcode. You can download it for free from Apple app store.
Then install Command line tools for Xcode from https://developer.apple.com/downloads/
Git
From the website
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
- Download and run installer from http://git-scm.com/.
- Restart bash/zsh sessions
- Run provided shell script to add PATH and MANPATH to your ~/.MacOSX/environment.plist file
Homebrew
Homebrew allows you to install Unix tools on OS X. A much better replacement of fink.
If your system has no issues, brew would return
Python
Lion already comes installed with version 2.7.1. However we will build virtual environment to isolate and install more than one versions of python as well as corresponding modules.
Prerequisites
readline: The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in.
gdbm: GNU dbm is a library of database functions that use extensible hashing and work similar to the standard UNIX dbm. These routines are provided to a programmer needing to create and manipulate a hashed database.
pkg-config : pkg-configis a helper tool used when compiling applications and libraries and helps you insert the correct compiler options on the command linerather than hard-coding values on where to find libraries. It is language-agnostic.
sqlite: SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
pip: A better package manager for Python
virtualenv
Virtualenv lets us install isolated Python environment, each with its own set of packages and dependencies. It is similar to rvm
in Ruby.
Lets install virtualenv and virtualenvwrapper (a tool to easily manage virtualenv enviroments).
To load virtualenv, we have to source /usr/local/bin/virtualenvwrapper.sh
script. We can automate by adding to ~/.bashrc
or in my case ‘~/.zshrc’ depending on the shell you use.
To use virtualenv refer http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html#command
Python tools
As numpy and scipy will fail without fortran compiler
Install NumPy
Install SciPy
Although OSX comes with the prerequisites for matplotlib installed, setup.py is not able to locate them. Therefore we’ll use
Now install matplotlib
R
R is a free software environment for statistical computing and graphics.
Link R
Enable rJava support
Last we’ll install iPython. If you like the default python shell I recommend a good ~/.pythonrc
. Here is a good starting point by Seth House.
Now Python shell is ready.
Workflow
After launching terminal, to move into virtualenv - ML
in our case
Listing installed packages installed in the virtualenv (pip packages stored here for env ML ~/.virtualenvs/ML/)
Install/Upgrade/Uninstall packages
Leave virtualenv