1

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?

Community
  • 1
  • 1
Westcroft_to_Apse
  • 1,503
  • 4
  • 20
  • 29

1 Answers1

8

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thanks for such a full and eloquent answer to my question! But actually, I do have `cairo` installed - albeit with MacPorts (as per the recommendation in the docs). If the isolationism of MacPorts is the problem, should I now install Homebrew just for this purpose? – Westcroft_to_Apse Dec 18 '12 at 19:55
  • 1
    @Westcroft_to_Apse: If you're already extensively using MacPorts, I'd suggest sticking with it, and installing and using MacPorts' python instead of Apple's. IIRC, they actually have a `pycairo` port, so the whole thing should be just a matter of `sudo port install py27-pycairo` or something similar. On the other hand, if you're not using MacPorts for anything, and only installed it to install `cairo`, I'd suggest uninstalling it and switching to Homebrew. – abarnert Dec 18 '12 at 20:00
  • I got a lot of warnings when I installed Homebrew and ran `brew doctor`. This is a pain. On the one hand, I'd rather not move and potentially break MacPorts, etc, on the other hand, I'd rather not shift to using the MacPorts Python unless I have to... I'm sure I'm not the first person to say so, but THIS IS INSANE! I only want Cairo so that I can have Pycairo, and I only want Pycairo so I can use Igraph, and I only want Igraph so that I can create a dendrogram using the walktrap algorithm. If not for that single feature, I'd be quite happy with NetworkX and Matplotlib and all would be well. – Westcroft_to_Apse Dec 18 '12 at 20:59
  • @Westcroft_to_Apse: Yes, it's annoying. You pretty much have to commit to "Apple stuff + Homebrew" or "MacPorts stuff only" early on (before you even know there's a choice…) or you're hosed. Anyway, building `cairo` from source (and installing to `/usr/local`) isn't that hard, which is a third option, but it's really not that much different from using Homebrew (and Homebrew isn't really adding new problems, it's just warning you about the same problems you may or may not have). Anyway, it isn't _impossible_ to build Apple-`python` `pycairo` with MacPorts `cairo`, it's just not supported… – abarnert Dec 18 '12 at 21:16
  • I'm accepting your answer even though I haven't managed to fix the problem, because you clearly know what you're talking about. In the long term I think the answer is probably to ditch all the stuff I've installed with MacPorts and start over with Homebrew, but in the short term I think it might actually be easier to switch from Python to R for this particular task. – Westcroft_to_Apse Dec 18 '12 at 21:36
  • On the other hand, the worry I have with the warnings is that they are so vague, e.g. `You have MacPorts or Fink installed... This can cause trouble. You don't have to uninstall them, but you may want to temporarily move them out of the way` - what sort of 'trouble'? How long is 'temporarily'? If I move 'them' (actually just MacPorts) back afterwards, when should I move them out of the way again? (Questions directed more at the software than at you, abarnert, btw! I'm just so frustrated with all this.) – Westcroft_to_Apse Dec 18 '12 at 21:39
  • @Westcroft_to_Apse: "Temporarily" means "at least while running `brew install`, and maybe again while using it (i.e., each time you run Apple Python and want to import pycairo)". But it's not guaranteed to be a problem, and it's hard to predict when it will be. So there are two options: try first, and if things don't work, then move MacPorts out of the way, or always proactively move MacPorts out of the way to be safe. The first has the disadvantage that things may _seem_ to work but actually have problems; the second that it's, obviously, a pain in the neck to do. – abarnert Dec 18 '12 at 21:44
  • @Westcroft_to_Apse: One last point. I had a system with Fink, MacPorts, and Homebrew _all_ installed, and I used a script to set up the PATH and other variables to just use one at a time… It _is_ possible to work this way. But it's horribly painful; in the end, it was simpler to wipe out all three and start over with just Homebrew (and figure out how to install the two packages that Fink had and Homebrew didn't… which turned out to be pretty easy, just asking brew to guess the right recipe). – abarnert Dec 18 '12 at 21:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21348/discussion-between-westcroft-to-apse-and-abarnert) – Westcroft_to_Apse Dec 18 '12 at 21:54
  • pydanny has steps for Lion and Homebrew. http://pydanny.com/installing-pycairo-on-mountain-lion.html This has worked well on my freshly installed Yosemite. However, I'm having problems with my MBA that has Yosemite. – Max Oct 23 '14 at 04:47