views:

423

answers:

2

This is the first time I've ever touched python, I'm trying to use a library 'cairo', but have been unable to import it getting the message

Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cairo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cairo

From what I understand I have installed all the correct libraries, detailed as follows:

1) Installed Python2.6.4 from the .dmg found http://www.python.org/download/releases/2.6.4/

2) Installed cairo1.8.8 from macports 'sudo port install cairo'

3) Installed py-cairo from macports 'sudo install py-cairo' (After not being able to import cairo)

4) Installed py-cairo from source tarball 'http://www.cairographics.org/pycairo/'

./configure
make install
python setup.py install

Now there is a directory /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/cairo which has been created but it only contains '_cairo.so' and I still cannot import cairo from the python interpreter.

Is there something obvious I did wrong...? Any help is appreciated.

+2  A: 

The port command installs the library for the darwinports python installation, which is different to the framework build (so steps 2 and 3 shouldn't work). Try sudo easy_install pycairo instead (although your step 4 should be equivalent to this).

Look at which python too, to check that you are in fact running the python you think you are.

Michael Dunn
Here (http://stackoverflow.com/questions/1500866/how-to-install-pycairo-on-osx) is another person with a similar problem
Michael Dunn
If you are not careful, using easy_install like that could further complicate matters by bringing the Apple-supplied python into the picture and it won't help solve the root problem.
Ned Deily
I took Akusete to mean that he wanted to use pycairo from his Python2.6.4 framework build (his step 1). If he gets that working he could ditch all the MacPorts python stuff and avoid this problem in the future. But your all-MacPorts solution is good too. Is there any reason to prefer it?
Michael Dunn
Getting all the dependent libraries straight and linked properly can be tricky (I haven't looked at the pycairo installation files to see if they check for libraries in the MacPorts locations). More importantly, by default, a plain `easy_install` command will most likely invoke the Apple-supplied /usr/bin/easy_install command which will install pycairo in the site-packages directory of the Apple-supplied default Python, not the python.org 2.6.4; to do that, you'll need to install a separate easy_install (from setuptools or Distribute) for that python.
Ned Deily
+2  A: 

It appears you are mixing various install options here. The MacPorts package system port install command should automatically pull in all the dependencies needed for a particular package so the trick is to start with the right top-level project. For python packages, MacPorts has a general convention currently: packages that start with py- are for python 2.4, those with py25- are for 2.5, and py26- for 2.6. There are currently py-cairo, py25-cairo, and py26-cairo packages available in MacPorts.

By choosing py-cairo you picked the python2.4 version and you'll probably find that MacPorts built and installed a python2.4 for you (linked at /opt/local/bin/python2.4) and, if you launch it, you'll probably find that you can import cairo there. Now that may be OK for your needs but Python 2.4 is quite old and no longer supported so, if you're just starting, it might be better to start with Python 2.6, one of the two current versions of Python. To do so, all you should need to do is:

sudo port install py26-cairo

That should bring in any missing dependencies, mainly the MacPorts python2.6, which you can run from /opt/local/bin/python2.6. You may want to change your $PATH in your shell startup script, probably .bash_profile, to put /opt/local/bin early on the search path.

Because installing Cairo and its python bindings seems to be fairly complex, it should be easier and better to stick to using a complete MacPorts solution for this. That does mean you've needlessly (and harmlessly) installed a couple of Python instances that you won't need. But if you do want to clean things up a bit, you can easily remove the MacPorts python24 with:

sudo port uninstall py-cairo python24

Completely removing the python.org installed python is more complicated. I've explained the process here. But there's no pressing need to remove either as long as you keep your paths straight.

Ned Deily
I tried your suggestion and it worked successfully. Thank you very much. I think my main problem was that I am unaware of the mechanisms and conventions of macports and macos in general.I have read macports introductions and documentation from macports.org but I never realised the version number was used in the title (I assumed it somehow used resolved to the latest version or something)
Akusete
Yes, the MacPorts python naming convention is rather confusing. If they had started by using py24- instead of py-, it might be less so. Glad it all worked!
Ned Deily