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 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
Next you'll want to ensure that conda and Python up to date:
$ conda update conda
Certain packages are essential for almost every Python user. These are easily installed via conda:
$ conda install pip numpy scipy matplotlib pandas
Matplotlib will probably install but you might find it raises an error when you actually try to use it:
>>> 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
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
$ 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 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 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
However, the important thing is that extended image support has been installed. You can check this by doing the following:
If pygame.image.get_extended() returns 0 or False, you are stuck with basic image support.