Mac OS Python Installation for Scientific Computing
This gist describes how to install the basic tools you'll need to do scientific computing with Python on Mac OS X. These have been tested with Mac OS X 10.6 and 10.7.
Your Mac comes with Python installed by in
/usr/bin/; however, you
probably don't want to use that Python because it is rarely updated
and, it's not a good idea to mess with that which OS X has installed.
So, most Python developers on Macs install a copy of Python with homebrew, a 3rd party package manager (that you're probably already using). I'll show you how to do that here.
If you don't have homebrew installed, install it as such
ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
To use software installed by Homebrew, you'll want to alter your path, usually in your .baschrc or something similar
Get the OSX command line tools
At this point, we need to ensure that you have the appropriate compilers on your syste: Apple's "command line tools". (If you've been using homebrew for a while, you've probably already done this step). Your Mac does not include the command line tools by default; instead, they are part of Xcode. There are two ways to get the tools. The first is to install Xcode from the AppStore, then install the "command line tools" using the Components tab of the Downloads preferences panel in Xcode. The second way to install the command line tools is to download them from the Apple developer center. That can be a much faster way. Neither way is better for our purposes. But, if you plan on doing OSX/iOS development, go ahead and get the full XCode.
Install Python via homebrew
Now, install Python, building it as a "Framework" for OS X
brew install python --framework --universal
After that, if you plan to use this Python as the default in your path, you'll need to update your "Framework Python" symlink. That should be something like
cd /System/Library/Frameworks/Python.framework/Versions sudo rm Current ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current
I say "something like", becase your version, e.g. "2.7.3", may be different.
Install common dependencies
Many scientific computing libraries in Python are actually written in C under the hood, and even Fortran. So, you'll need to install some binary dependencies. We'll do that with homebrew.
brew install gfortran # for scipy brew install pkg-config # for matplotlib brew install zmq # for ipython
Install pip and virtualenv
Now let's make sure pip is installed. Do
which python. If it says
your PATH is set up correctly. If not, make sure you fix it (see above).
Now, install pip, which is a tool for installing Python packages.
From now on, we'll use
pip instead of
easy_install. Then, install virtualenv so that we can create different Python environments for each project we work on, similar to
rvm in Ruby.
pip install virtualenv
Create your first project
OK - now you're ready to install some fun packages for scientific meyhem in Python. This typically goes something like as follows. First, create a directory
mkdir myproject cd myproject
Then create a new virtual environment called
virtualenv --distribute myvenv
Now "activate" that virtual env by sourcing the appropriate file. Assuming you're in the same directory
Now, when you type
which python you should see something that includes
which means you're using the virtual environment and that you're free to install
all sorts of Python packages willy-nilly because they won't effect your other
Here's how you install some basic packages you may want
pip install numpy pip install scipy pip install ipython pip install pandas pip install scikit-learn
There it is. Now you've got the basic set of tools that most people use to write machine learning code in Python. You should be able to start up an Ipython notebook like this
ipython notebook --pylab inline
Enjoy! If you're part of NewHaven.io, don't hesitate to contact me with questions.
Now you're ready to start a Python project!