Python scientific computing on Mac OS X Yosemite

Years ago I did web server admin on FreeBSD. It has a very good package management system called Ports. It provides a consistent way to install/uninstall apps and helps you install all the required components, or dependencies, so you don’t need to worry about them. Most Linux distributions come with similar system, such as dpkg for Debian/Ubuntu and RPM for Red Hat.

On Mac OS X, I use Homebrew. You can install many apps in the collection from source code and it does similar things like finding dependencies and giving a clean install/uninstall process. I really appreciate this when trying to build a scientific computing environment with Python. For example, I can simply run brew info matplotlib to find out what dependencies are required or optional.

brew_info

Many modules in Python depend on other modules, for example, Numpy is a dependency of both Scipy and Matplotlib. You can certainly imagine how much trouble it will be to download the binary copies of Python, Numpy, Scipy, Matplotlib, all separately from their official website, and install them — assuming you already have a list of all the dependencies they require.

Python has its own package manager called pip. It takes care of the above mentioned problem and install all the dependencies for you. Homebrew does similar thing, but it is not limited to just Python. You can install many apps with Homebrew and updating them is very easy. One of the best things about Homebrew is that it stores everything in the /usr/local/ folder and leaves other system folders clean. Installing Homebrew is as simple as:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

And you can start using it. I will run brew doctor and brew update before I install any app just to make sure everything is working and I have the newest recipes available.

brew doctor
brew update
brew install python

Now run which pythonand verify that your system is using /usr/local/bin/python, otherwise you need to fix it by editing the paths setting in ~/.bashrc or ~/.profile. Once you have done that, continue to install the necessary Python modules for scientific computing. Note that some modules does not have Homebrew recipe, and you need to use pip to install them.

pip install virtualenv
pip install nose
brew install numpy
brew install scipy
brew install matplotlib
pip install ipython[notebook]

The above modules are the very essential tools for scientific computing tasks, such as matrix algebra and curve plotting. IPython with notebook support provides a convenient way to write plain-text or equations together with the Python code. It looks a bit similar to Mathematica, and I encourage you to try it out.

Sometimes things can go wrong during the installation process. For example, when I tried to use Homebrew to update my Scipy and Numpy, I got the following error message, saying Homebrew failed to link the newly compiled Scipy.

brew_link

This is because I installed the older version of numpy and scipy with pip, which leaves some files in the system that are conflict with Homebrew. To fix this, I simply do brew link ––overwrite scipy to force the link and overwrite the existing files. However, it is a good idea to backup your system before you do this.

Using Python for scientific computing is getting more and more convenient and powerful with the continuing development of Numpy/Scipy/Matplotlib. To find out more about scientific computing using Python, check the following articles:

  • Scientific Python on Mac OS X 10.9 with Homebrew
  • Wow ! Much Homebrew. Very Numpy. So Scipy. Such OpenCV
  • Advertisements

    One thought on “Python scientific computing on Mac OS X Yosemite

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s