33

I've tried to install pycairo in a virtualenv to use in a Django project. I've ran the pip install pycairo==1.10.0 command which finds the package and downloads it unlike other commands like pip install pycairo, etc. but when starting to install the package it throws an error.

Here's the log:

Downloading/unpacking pycairo==1.10.0
  Running setup.py egg_info for package pycairo
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
    IOError: [Errno 2] No such file or directory:
'/home/radu/Desktop/djangos/workout/venv/build/pycairo/setup.py'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 14, in <module>

IOError: [Errno 2] No such file or directory:
'/home/radu/Desktop/djangos/workout/venv/build/pycairo/setup.py'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in 
/home/radu/Desktop/djangos/workout/venv/build/pycairo
Storing complete log in /home/radu/.pip/pip.log

Could you please give me any hints about what to do? Should I try and write a setup.py file for the package and then try to install it? (i'm not sure it's even a solution, i still am trying to figure out what I can do).

Thanks in advance!

Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107

6 Answers6

67

Good news, everyone!

I just released cairocffi: http://packages.python.org/cairocffi/

It’s a replacement for pycairo that installs with pip in a virtualenv, runs on Python 2 and 3, as well as PyPy.

pip install cairocffi

In your code:

import cairocffi as cairo
# Enjoy the same API as Pycairo.

Feedback welcome. (Although the issue tracker might be a better channel than here.)

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
  • 1
    This is most excellent, and also seems to be better documented than pycairo. – David Eads Mar 14 '13 at 16:48
  • This installed fine but rendered completely wrong for me. Sorry, don't have time to isolate the problem and file a bug report, but wanted to leave this here for others. – Vebjorn Ljosa May 12 '13 at 03:26
  • 1
    @Vebjorn Ljosa, A bug report with sample code and expected / actual result would be much appreciated, even if you do not isolate the problem. Right now I have no idea how to help :/ – Simon Sapin May 12 '13 at 10:53
  • 1
    @SimonSapin, yes, I understand and I apologize. And, for the record, it's entirely possible that I'm doing something off spec. I wish I had time to isolate a test case. – Vebjorn Ljosa May 13 '13 at 05:47
  • 7
    Works! After a lot of struggle with cairo this proved to be the easiest solution to get it to work. Just make sure you have libffi-dev installed ($ sudo apt-get install libffi-dev) – odedfos Feb 17 '14 at 16:20
  • don't forget to yum install libffi-devel and python-devel – jedierikb Dec 11 '15 at 21:13
  • I'm afraid I'm getting the error `OSError: dlopen() failed to load a library: cairo / cairo-2` when I do `import cairocffi as cairo` – sachinruk May 19 '16 at 06:16
  • Thanks, just solved my cairo problem with ggplot in Python 3.4.3 (Ubuntu 15.04) $ sudo pip3 install cairocffi – AAAfarmclub Jul 04 '16 at 00:05
  • `apt-get install libffi6 libffi-dev ` for ubuntu --https://stackoverflow.com/questions/38109637/package-libffi-was-not-found-in-the-pkg-config-search-path-redhat6-5 – qxo Jul 25 '17 at 12:18
  • Hello. I am installing `manim` that requires `pycairo`. After a lot of struggle i found this answer that worked for the purpose of installation. But the examples in `manim` package does show nothing when i execute the commands. Do you think this could be related still to `pycairo` or i should find the bug somewhere else like in `manim` ? – moctarjallo Dec 13 '19 at 23:21
24

Although py2cairo doesn't install nicely using pip, you can still install py2cairo into the virtual environment using the build instructions in the INSTALL file from the distribution.

You will need the cairo-dev/cairo-devel package for you os installed in order to build the package.

Do the following to install into your virtual environment:

  1. download, unpack, and cd into the the py2cairo directory
  2. Activate your virtual environment
  3. Follow the standard build procedure

./waf configure --prefix=$VIRTUAL_ENV

./waf build

./waf install

tgmauch
  • 241
  • 2
  • 2
  • 1
    This worked for me, except I had to do the following first: "export PYTHON=$VIRTUAL_ENV/bin/python". For whatever reason, entering the virtualenv does not set the global $PYTHON variable. – Ben Davis Nov 20 '12 at 23:16
  • Getting this error: `Checking for 'cairo' >= 1.10.2 : not found. The configuration failed` Installed cairo: 1.12.16_1 via brew. – Ben Jun 12 '14 at 16:06
  • 2
    During configure step I get the error `command ['/path/to/venv/bin/python', '/usr/bin/python2.7-config', '--includes'] returned 1` immediately after `Checking for program python2.7-config`. Anyone know how to fix this? – fpghost Aug 06 '14 at 14:00
  • 4
    @MicheleGargiulo The only way I found to get this working was using the git dev branch `git clone git://git.cairographics.org/git/py2cairo` and installing with the `autogen.sh --prefix=/path/to/venv;make; make install` script . It seems the version of `waf` pycairo uses is outdated and still thinks the `python2.7-config` is a python script not bash; even the patch didn't help for me. – fpghost Aug 12 '14 at 20:13
  • 1
    @fpghost got it working with autogen.sh on virtualenv on Ubuntu 14.10, Python 2.7.8 – Burnash Mar 03 '15 at 00:55
11

pycairo currently does not support installation through pip/distutils. The project’s install docs instructs to use either waf or autotools.

To use pycairo in a virtualenv, you need to:

  • Install pycairo system-wide, preferably through your distribution’s packages
  • Then, either:
    1. Create a virtualenv with the --system-site-packages option or remove the lib/pythonX.Y/no-global-site-packages.txt file after the fact.
    2. Or add a symbolic link to the cairo package (the directory containing _cairo.so). Something like this:
      ln -s /usr/lib/python2.7/site-packages/cairo ./venv/lib/python2.7/site-packages
      

Of course 1. has the downside that you will not profit from virtualenv’s isolation from other packages installed system-wide.

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
  • 1
    The [git version of pycairo](http://cgit.freedesktop.org/pycairo/log/setup.py) (For Python 3) recently got a setup.py and installs perfectly with pip. Hopefully the next version will solve all this (and in py2cairo for Python 2 as well.) – Simon Sapin Aug 21 '12 at 20:13
4

For anyone trying to use pycairo (for Python 2.7) in conjunction with Homebrew and virtualenv --no-site-packages ... this Worked For Me:

  1. brew install py2cairo

  2. Then, find the path where Homebrew installed it to, will be something like:

    ls -l /usr/local/lib/python2.7/site-packages/cairo/
    total 24
    lrwxr-xr-x  1 anentropic  admin   80 10 Jun 14:26 __init__.py -> ../../../../Cellar/py2cairo/1.10.0/lib/python2.7/site-packages/cairo/__init__.py
    lrwxr-xr-x  1 anentropic  admin   78 10 Jun 14:26 _cairo.so -> ../../../../Cellar/py2cairo/1.10.0/lib/python2.7/site-packages/cairo/_cairo.so
    
  3. You want to find the path at the base of those symlinks, something like:
    /usr/local/Cellar/py2cairo/1.10.0/lib/python2.7/site-packages

  4. Then create a .pth file in your virtualenv site packages:
    echo "/usr/local/Cellar/py2cairo/1.10.0/lib/python2.7/site-packages" > venv/lib/python2.7/site-packages/cairo.pth

(may want to deactivate and re-activate your virtualenv for good luck, not sure)

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 1
    tested on OS X Mavericks. works a charm. Also solves the Graphite "ImportError: Could not find Cairo" issue. – Ben Jun 12 '14 at 16:40
  • on OS X 10.8, after installing and trying to import cairo, it gives me Fatal Python error: PyThreadState_Get: no current thread Abort trap: 6 – Burnash Feb 27 '15 at 02:38
1

If cairocffi installation in virtualenv does not work and python-dev libffi-dev are unavailable (ArchLinux) setting environmental variable PKG_CONFIG_PATH=/usr/lib/libffi-3.2.1/include works.

Anna Avina
  • 41
  • 5
-1

If you are using Homebrew you can install the pycairo (Python 3+) and py2cairo (Python 2.6-2.7) recipes:

brew install pycairo

luni3359
  • 25
  • 6
Hakan B.
  • 2,319
  • 23
  • 29