Installing Python packages on Mac OS X, Mountain Lion

To install matplotlib, a graphing package, I ran:

sudo easy_install pip
pip-2.7 install --user matplotlib

It seems to have worked:

me@Gris:~$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib

This was based on excerpted suggestions from: http://stackoverflow.com/questions/1213690/what-is-the-most-compatible-way-to-install-python-modules-on-a-mac

Sadly, this turns out not have worked after all.  Instead, when I try to actually use matplotlib, I get:

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/pyplot.py", line 26, in <module>
from matplotlib.figure import Figure, figaspect
File "/Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/figure.py", line 32, in <module>
from matplotlib.image import FigureImage
File "/Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/image.py", line 22, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/_png.so, 2): Symbol not found: _png_create_info_struct
Referenced from: /Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/_png.so
Expected in: flat namespace
in /Users/bparno/Library/Python/2.7/lib/python/site-packages/matplotlib/_png.so

Based on these posts:

http://astropython.blogspot.com/2012/08/how-to-install-ipython-scipy-and.html

http://www.thisisthegreenroom.com/2011/installing-python-numpy-scipy-matplotlib-and-ipython-on-lion/

I tried installing the dev versions via:

sudo pip install git+https://github.com/scipy/scipy#egg=scipy-dev
sudo pip install git+https://github.com/matplotlib/matplotlib.git#egg=matplotlib-dev

but those appear to have failed in the same manner.

Thus, I decided to start fresh with a HomeBrew approach.  Installed HomeBrew via:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

After it installed, I tried running “brew doctor”, which came up with several warnings.  One pointed out that Xcode was out of date, so I installed the update (side note: when I attempted to install the update, the app store app wouldn’t let me enter my new Apple  ID.  Choosing the “Log in” option from the “Store” menu did, however).

Install brew’s version of python:

brew install python --framework --universal

It mostly succeeded, but failed with:

Warning: Could not link python. Unlinking...
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link python'
Warning: Could not link python. Unlinking...Error: The `brew link` step did not complete successfullyThe formula built, but is not symlinked into /usr/localYou can try again using `brew link python'

It appears to be a result of my previous installation of pip:

brew link python
Linking /usr/local/Cellar/python/2.7.3... Warning: Could not link python. Unlinking...
Error: Could not symlink file: /usr/local/Cellar/python/2.7.3/bin/pip-2.7
Target /usr/local/bin/pip-2.7 already exists. You may need to delete it.
To force the link and delete this file, do:
brew link --overwrite formula_name
To list all files that would be deleted:
brew link --overwrite --dry-run formula_name
brew link pythonLinking /usr/local/Cellar/python/2.7.3... Warning: Could not link python. Unlinking...
Error: Could not symlink file: /usr/local/Cellar/python/2.7.3/bin/pip-2.7Target /usr/local/bin/pip-2.7 already exists. You may need to delete it.To force the link and delete this file, do:  brew link --overwrite formula_name
To list all files that would be deleted:  brew link --overwrite --dry-run formula_name

I used “sudo easy_install -mxN pip” to uninstall pip (I think), and then tried “brew link python” again.  Still didn’t work, since the pip executables were still in /usr/local/bin, which I found out via “brew link –overwrite –dry-run python”.  I went ahead and forced the issue with “brew link –overwrite python”.  Started a new Terminal and confirmed it was running Python 2.7.3.  Now try installing numpy and matplotlib again.

pip install numpy
pip install git+https://github.com/matplotlib/matplotlib.git#egg=matplotlib-dev

This appears to have actually worked this time around!

To celebrate, I installed SciPy as well via:

brew install gfortran
pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev

Leave a Reply