The Cracked Bassoon

Sam Mathias

I'm a psychoacoustician. I mainly post code for making interesting sounds.

27. May 2015 815 words ~6 minutes read 3 Comments

Python for psychophysics on OS X Yosemite

Apple Macs and Python are, of course, both generally fabulous, but make an especially formidable hardware/software combination for psychological or psychophysical research. Apple uses pretty high-quality internals for audio and video that should be adequate for running most experiments. Python is a highly readable, free and open-source language, promoting replicability. Moreover, Python has extensive data-analysis capabilities rivalling proprietary software like MatLab, meaning that psychophysical data can be both collected and analysed easily within the same framework.

It’s been my experience that getting Python and its various third-party packages to run on a Mac is more trouble than it really ought to be. All-in one bundles like Enthought Canopy and Anaconda are great options, but they tend to install dozens of additional packages you may never use, and it can be tricky to incorporate non-standard packages like Pygame. By contrast, manual compiling gives you much more freedom, but may make you want to throw your beautiful shiny Mac out the nearest window.

Here I’ve outlined my trials and tribulations installing Python on a Mac. It is highly idiosyncratic and in no way intended to be a guide for the optimal installation procedure. However, I have encountered numerous issues and have largely managed to fix them, so the following may be useful to others. I intend to keep this post regularly updated as encounter more issues in the future.

Miniconda

Miniconda is the minimalist version of Anaconda by Continuum Analytics. It contains the latest version of Python (either 2.7 or 3, at the time of writing) and Continuum’s own package manager, conda. Installation is very quick and easy: go to the Miniconda website and download the appropriate shell script; open a terminal; the cd into the download directory and run the script via bash:

$ cd Downloads/
$ bash Miniconda-latest-MacOSX-x86_64.sh

Miniconda prepends itself to your path, making itself your default Python installation (after you restart your terminal). You can test this by typing:

$ which python
/Users/smathias/miniconda/bin/python
...

Next you'll want to ensure that conda and Python up to date:

$ conda update conda

Essential packages

Certain packages are essential for almost every Python user. These are easily installed via conda:

$ conda install pip numpy scipy matplotlib pandas

Fixing Matplotlib

Matplotlib will probably install but you might find it raises an error when you actually try to use it:

$ python
...
>>> import matplotlib.pyplot as plt
...
ImportError: dlopen(/Users/smathias/miniconda/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Library not loaded: @loader_path/../../../libfreetype.6.dylib
Referenced from: /Users/smathias/miniconda/lib/python2.7/site-packages/matplotlib/ft2font.so
Reason: Incompatible library version: ft2font.so requires version 18.0.0 or later, but libfreetype.6.dylib provides version 16.0.0

In my case this error occurred because libfreetype.6.dylib was out of date.

$ otool -L /Users/smathias/miniconda/lib/libfreetype.6.dylib
/Users/smathias/miniconda/lib/libfreetype.6.dylib:
libfreetype.6.dylib (compatibility version 16.0.0, current version 16.0.0)
...

The solution was to find a more up-to-date version on my hard drive and overwrite the one in the Miniconda directory:

$ sudo find / -name libfreetype.6.dylib
...
/opt/X11/lib/libfreetype.6.dylib
$ cp /opt/X11/lib/libfreetype.6.dylib /Users/smathias/miniconda/lib/libfreetype.6.dylib

PySide or PyQt

Qt is really great software for building GUI-based apps. Python has two packages that allow users to write Qt applications directly in Python: PySide and PyQt. Since their syntaxes are almost identical, the same code will work with either package 99.9% of the time. I’ve found that PySide is slightly more straightforward to install on the majority of platforms:

$ conda install pyside

When writing a script that uses Qt, I include a try/except statement that imports either package.

Pygame

Pygame is the perfect package for quickly programming psychological experiments. It was designed for writing amateur games in Python using the SDL library, and provides convenient objects for drawing to the screen, presenting audio and video, collecting user input, and so on. It also has a surprisingly large user community which makes it easy to find solutions to many problems. However, the installation procedure on a Mac is quite painful. The first major issue it has several annoying dependencies, and unfortunately, the versions of these supplied by conda aren't suitable. To install the correct dependencies, we need to use Homebrew:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install sdl sdl_ttf sdl_image sdl_mixer portmidi

The second issue is that the official Pygame repo at Bitbucket doesn't build properly on OS X. Specifically, it fails to find the libraries required to provide extended image support, preventing Pygame from displaying any images besides uncompressed bitmaps. This is far from ideal. Fortunately, there is a binstar version that does seem to build properly, at least the last few times I have tried it.

$ conda install binstar
$ conda install -c https://conda.binstar.org/quasiben pygame

A caveat here: although this version is labelled 1.9.1, its actually 1.9.2a0! You can verify this by opening python and typing the following:

$ python
Python 2.7.9 |Continuum Analytics, Inc.| (default, Dec 15 2014, 10:37:34)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import pygame
>>> pygame.__version__
'1.9.2a0'

However, the important thing is that extended image support has been installed. You can check this by doing the following:

>>> pygame.image.get_extended()
1

If pygame.image.get_extended() returns 0 or False, you are stuck with basic image support.

3 Comments

  1. Rob Stone

    Hi Sam

    long time...

    thanks for this i've been fighting anaconda to get pygame installed, what a pain but eventually i now have a 2.7 working with pygame etc and a 3.5

    • smathias

      Thanks, glad it worked! It's been a while since I've had to do this, so wasn't sure if the recipe was still working.

Leave a Reply

Your email address will not be published. Required fields are marked *