All posts All posts by this author Paper color Change page color Announcements

Installing PyCairo in virtualenv (Debian/Ubuntu)

Googled a lot to find a solution for installing PyCairo in virtualenv but ended up without any perfect solution. Hope my solution gonna help you a bit.

Create Virtualenv

# for python2
$ virtualenv -p python2 env
# for python3
$ virtualenv -p python3 env

Activate Virtualenv

$ source env/bin/acivate

Download and Extract PyCairo

python 2.x (>=2.6)

(env)$ wget -c http://cairographics.org/releases/py2cairo-1.10.0.tar.bz2
(env)$ tar jxvf py2cairo-1.10.0.tar.bz2 
(env)$ cd py2cairo-1.10.0

python 3.x (>=3.1)

(env)$ wget -c http://cairographics.org/releases/pycairo-1.10.0.tar.bz2
(env)$ tar jxvf pycairo-1.10.0.tar.bz2
(env)$ cd pycairo-1.10.0

waflib problem fix

replace waf faile

(env)$ wget -c --no-check-certificate https://ftp.waf.io/pub/release/waf-1.8.7 -O waf
(env)$ chmod 777 waf

replace wscript

(env)$ vi wscript
# -*- python -*-

import os

top = '.'
out = 'build_directory'
d = top

APPNAME='py2cairo'
VERSION='1.10.0'
cairo_version_required = '1.10.0'


def options(ctx):
  print('  %s/options()' %d)
  ctx.load('gnu_dirs')
  ctx.load('compiler_c')
  ctx.load('python') # options for disabling pyc or pyo compilation


def configure(ctx):
  print('  %s/configure()' %d)

  env = ctx.env
  ctx.load('gnu_dirs')
  ctx.load('compiler_c')
  ctx.load('python')
  ctx.check_python_version((2,6,0))
  ctx.check_python_headers()
  ctx.check_cfg(package='cairo', atleast_version=cairo_version_required,
                 args='--cflags --libs')

  # add gcc options
  if env['CC_NAME'] == 'gcc':
    env.append_unique('CCFLAGS', ['-std=c99', '-Wall'])

  version = [int(s) for s in VERSION.split('.')]
  ctx.define('VERSION', VERSION)
  ctx.define('PYCAIRO_VERSION_MAJOR', version[0])
  ctx.define('PYCAIRO_VERSION_MINOR', version[1])
  ctx.define('PYCAIRO_VERSION_MICRO', version[2])

  ctx.write_config_header('src/config.h')

  print("Configuration:")
  print("%-40s : %s" % ('PREFIX', env['PREFIX']))
  print("%-40s : %s" % ('LIBDIR', env['LIBDIR']))


def build(ctx):
  print('  %s/build()' %d)
  ctx.recurse('src')

  # generate and install the .pc file
  ctx(
    features = 'subst',
    source   = 'pycairo.pc.in',
    target   = 'pycairo.pc',
    dct      = {
      'VERSION'   : VERSION,
      'prefix'    : ctx.env['PREFIX'],
      'includedir': os.path.join(ctx.env['PREFIX'], 'include'),
      },
    install_path = os.path.join(ctx.env['LIBDIR'], 'pkgconfig'),
    )


def dist(ctx):
  # exclude these files from the distribution archive
  exclude  = (
    'build_directory/',
    'doc/_static/',
    'doc/_templates/',
    'doc/_build/doctrees/',
    'doc/_build/html/_sources/',
    'examples/cairo_snippets/c_to_python.py',
    'RELEASING',
    '.git/',
    '**/.gitignore',
    '**/.lock-w*',
    '.waf*',
    '**/*.pdf',
    '**/*.png',
    '**/*.pyc',
    '**/*.ps',
    '**/*.svg',
    # autoconf specific files
    '**/Makefile',
    'autogen.sh',
    'autom4*',
    'config.log',
    'config.guess',
    'config.sub',
    'depcomp',
    'install-sh',
    'missing',
    'py-compile',
    'src/.deps/',
    'src/.libs/',
    '**/*.lo',
    '**/stamp-h1',
    )
  ctx.excl = ' '.join(exclude)

Install

(env)$ ./waf configure --prefix=$VIRTUAL_ENV --libdir=$VIRTUAL_ENV/lib
(env)$ ./waf build
(env)$ ./waf install

Check installation by importing

(env)$ python -c "import cairo"
(env)$ :)
Software Solutions