Python setup with HomeBrew

HomeBrew offers a nice way of installing a Python distribution with a single command.  As usual, it puts the files releated to the installation in /usr/local/Cellar and symlinks the necessary things to /usr/local/[bin/lib/include], but it also is a bit more careful about the site-packages directory so that it and anything you install yourself doesn’t get removed if you upgrade the HomeBrew Python distribution, as described here: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python.  The result is that pip and easy_install put things in /usr/local/lib/python2.7/site-packages/.

To install:

brew install python

As described in the link above, to be able to use the HomeBrew pip and easy_install, you have to modify your PATH variable as follows, perhaps in ~/.bashrc:

export PATH="/usr/local/bin:/usr/local/share/python:${PATH}"

Numpy, Scipy, Matplotlib

These commonly-used python modules can be easily installed with the new HomeBrew Python installation.  Installing Numpy and Scipy is described here, and detailed below.

## Go to a directory where the downloaded files can be placed
cd /usr/local/src
## Download the module distributions
git clone https://github.com/numpy/numpy.git
git clone https://github.com/scipy/scipy.git

I had to add the following commands needed to get the compiler flags correct with gfortran and gcc, otherwise some of the fortran libraries were being complied with “i386″ architecture and though the modules would appear to be installed correctly, Python would complain later with errors like “mach-o, but wrong architecture because the library was compiled with i386″.

export MACOSX_DEPLOYMENT_TARGET=10.6
ARCH="-arch x86_64"
export CFLAGS="${ARCH}"
export FFLAGS="-static -ff2c ${ARCH}"
export LDFLAGS="-Wall -undefined dynamic_lookup -bundle ${ARCH}"

cd /usr/local/src/numpy
python setup.py build
python setup.py install

cd ../scipy
python setup.py build
python setup.py install

Other useful modules appeared to install simply with pip:

pip install ipython
pip install matplotlib
pip install pyfits
pip install sphinx
pip install cython

The result of this is a fresh, clean Python installation that can easily be upgraded and that is easy to install additional modules. I’m still using the STSCI_PYTHON distribution for other things, as I didn’t figure out what would be necessary to plug IRAF and PyRAF into the HomeBrew distribution of python. The two are kept separate by the fact that STSCI_PYTHON is all run in csh, setting all of the necessary environment path variables when you invoke that shell.

I noticed in the HomeBrew installation that there was a newer IPython version (0.12) than the version installed with STSCI_PYTHON (0.10). I used pip to upgrade the STSCI_PYTHON IPython as follows:

$ csh
% pip install --upgrade ipython

However then starting ipython in csh resulted in a rather terrifying error:

Traceback (most recent call last):
  File "/usr/stsci/pyssg/Python-2.7/bin/ipython", line 9, in 
    load_entry_point('ipython==0.12.1', 'console_scripts', 'ipython')()
  File "/usr/stsci/pyssg/2.7/distribute-0.6.10-py2.7.egg/pkg_resources.py", line 299, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/stsci/pyssg/2.7/distribute-0.6.10-py2.7.egg/pkg_resources.py", line 2229, in load_entry_point
    return ep.load()
  File "/usr/stsci/pyssg/2.7/distribute-0.6.10-py2.7.egg/pkg_resources.py", line 1948, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named terminal.ipapp

This resulted from the fact that the old IPython was still sitting around and conflicting with the upgrade. Removing the old version from the path as follows solved the problem:

cd /usr/stsci/pyssg/2.7
sudo mv IPython IPython.10 ## don't remove, just change the path

UPDATE! The above fix allowed me to start IPython with the STSCI_PYTHON, but now “pyraf –ipython” is broken. Be wary of updating the modules piecemeal within the STSCI_PYTHON distribution as some are customized for wrapping the “RAF” part of PyRAF.

UPDATE#2: Easily reverted to ipython-0.10 with

sudo pip install http://pypi.python.org/packages/source/i/ipython/ipython-0.10.tar.gz

and “pyraf –ipython” now appears to work as before.

About these ads
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Python setup with HomeBrew

  1. Pingback: Homebrew python: default interactive plotting with Matplotlib « OS X Astro Tricks

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s