pythonmacoscairoeasy-installpycairo

How to install pycairo for the Apple build of Python 2.7, using easy_install


I want to install the Python bindings for Cairo on Mac OSX Lion, so that I can use them with the Apple build of Python 2.7. I tend to install Python modules etc using easy_install, but that doesn't appear to work in this case:

$ sudo easy_install pycairo
Searching for pycairo
Reading http://pypi.python.org/simple/pycairo/
Reading http://cairographics.org/pycairo
Best match: pycairo 1.10.0
Downloading http://cairographics.org/releases/pycairo-1.10.0.tar.bz2
Processing pycairo-1.10.0.tar.bz2
error: Couldn't find a setup script in /tmp/easy_install-BYpm4Y/pycairo-1.10.0.tar.bz2

Similar questions have been asked elsewhere on Stack Overflow, but the answers do not explain what I'm doing wrong here.

This question relates to Python 2.6, the accepted answer uses Macports rather than easy_install, and the other answer appears to be advising the use of exactly the method I tried above (which failed):

How do you install pycairo on Mac OS X?

And this question relates to building pycairo from source, which I don't want to do:

How to install pycairo on osx?


Solution

  • First, you almost always want to use pip instead of easy_install whenever possible. And "whenever possible" pretty much means "for everything except pip itself, and readline." Apple doesn't pre-install pip, but you can just sudo easy_install pip and then everywhere you'd sudo easy_install foo instead sudo pip install foo.

    Second, pycairo requires Python 3.1+. You want py2cairo, the version for Python 2.6-2.7. Since there is no PyPI package for this, you have to either download it or git clone it manually. But fortunately, pip does this for you automatically: sudo pip install http://cairographics.org/releases/py2cairo-1.10.0.tar.bz2 or sudo pip install git+https://git.cairographics.org/git/py2cairo.

    Unfortunately, not everything can be installed by either pip or easy_install. If either one fails, you have to read the PyPI page, home page, or documentation for the project.

    In the case of pycairo, the reason it can't be easy_install/pip installed is that it's built using waf instead of distutils (and nobody has written a distutils wrapper around the waf build). But you don't really care why it doesn't work, just what you have to do.

    Well, the instructions tell you what you have to do. You may think you don't want to build from source, but that's exactly what pip usually does (and likewise easy_install), so it's not something to be afraid of.

    But all of this is secondary to the main problem: the pycairo Python module requires the cairo library (and likewise pixman), which you don't have. (Actually, depending on your OS X version, you might have a private version that can't be linked externally, or a very old version that's not good enough for pycairo, or no version at all… but any of those three are equivalent as far as you're concerned.)

    So you have to get it from somewhere before pycairo can be built. (And, even if you got a copy built somewhere else, you couldn't use it.)

    Contrary to what the docs suggest, I'd use Homebrew rather than MacPorts to install cairo. MacPorts is designed to build a sort of completely isolated set of tools—so it's great if you want to install cairo so you can install the MacPorts pycairo into your MacPorts /opt/local/bin/python, but it's not so great if you're hoping to get pycairo built for the stock Apple /usr/bin/python.