Geophysics Deskwork

Scott's Non-Field Notebook

Setting up Python for Scientific Computing on a Mac

The posts on this website are mostly related to my work as graduate student in geophysics. I was looking for a medium to easily keep records of my work and share computer code with colleagues, and naturally have settled into a combination of Github and this weblog (which coincidentally is hosted by Github). Since computers permeate all fields of science these days, I hope that the material I post here will be interesting and useful not just to geophysicists, but to many!

In my daily work I use a 2010 Macbook Pro running OSX 10.9 and a Linux Desktop running Ubuntu 13.04 (raring). The following recommendations are getting up and running with Python on a Mac.

Why use Python?

Python is a fantastic language for scientific computing! Here are some specific reasons why you might consider using it:

high-level interpreted language. What does that mean? It means the syntax is easy to read, and you can run code in a terminal window without pre-compiling! This is a huge advantage to languages like C and Fortran, which while fast, are a pain to compile and debug.

very versatile. I use Python to do numerical calculations, write file system scripts that require string processing, and publish this website! Imagine trying to do all this with Matlab or C...

I was convinced by those two points, but there are many other people out there who have put even more thought into convincing scientists to use Python:

Obtaining Python

If you have a Mac, you have Python already! It's accessible from the Terminal App type python. However, it's advisable to install another copy to customize and not risk messing up Mac OSX in any way.

There are a bunch of different ways to install full-featured Python. The most painless way is to utilize Enthought's pre-packaged Canopy distribution. It includes a minimal Matlab-like interface with a combined text editor and Python prompt, in addition to a very easy-to-use package management system. It's free for academic users, and they also have a free version with limited access to non standard Python libraries.

There are other options as well. I've tried Active Python in the past, but I really like Canopy. You can also check out the main Python page, which has an extensive list of installation options. For now, I recommend installing Python 2, since many libraries are still not compatible with the newer Python 3 series. See this article for more info on Python 2 versus 3.

Recommended Text Editors

Python comes standard with IDLE, a very basic code editor. Also, if you download Canopy, its built-in editor is pretty convenient. I find myself using several different editors day to day. Everyone has their favorites, and mine happen to be:

  • vi: The standard command line editor. You may need to enable syntax highlighting and tab-preference on your Mac:

    vi ~/.vimrc
    syntax on
    set ts=4
    
  • Komodo Edit: The myriad features seem like overkill sometimes, but some of them are handy

  • iA writer: I'm writing this document with iA writer. It's fantastic for simple stuff and webpages written in Markdown... but it's not free :(

Useful Python references

I've been using Python for 5 years now, and I have loved using it every step of the way! My first job involved creating some simple graphical user interfaces with Python, in which I heavily relied on these two books:

Of course, there are tons of tutorials on the web! I recommend learning by example with iPython notebooks. The notebook is very intuitive, but I definitely benefitted from watching some video tutorials by the developers of iPython. Otherwise you can get your hands dirty with official examples or lots of other interesting examples in this gallery.

Why to consider using Github

I didn't know what Github was until this year. But I started noticing a lot of software I used was 'moving to Github' (e.g. iPython, Pylith). I quickly learned that it is a relatively new (est. 2008) website based around the Linux version control utility, git, which has been around for decades! So why is this website getting so much buzz? It is a great tool for collaborative software development that encourages and facilitates writing well-documented open software.

I've always postponed learning version control software (e.g. svn) because it seemed complicated and tedious for the mostly small programs I write. However, I've learned over the years that these small programs can get quite big before you know it! And even small programs deserve clear commenting and accessibility. There are some people that have beaten me to this realization, so I'll refer you their work:

Comments