AlexMatan Bringing order to chaos and chaos to order since 1996.



Cairo Integration with wxPython - Installation of Python, wxPython, pyCairo, and Cairo 2-D graphics library on Windows XP

21 Nov 2009
Posted by Alex Matan

I wanted to get the Cairo 2-D graphics library to work within wxPython. I spent a few hours trying to figure out how to get the pyCairo package properly installed in Python on my Windows XP machine, so I figured I would post a detailed procedure for everyone's enjoyment.

The procedure takes about 15 minutes from not even having Python installed on your system to being up and running with Python, wxPython, pyCairo, wxCairo, and Cairo. It's pretty much entirely drag-and-drop, but I have included extra details for those who may not know what is actually going on but would like to know.

What we are working with: Python is an interpreted programming language. The wxPython package provides a cross-platform GUI library for Python. On Windows machines, Cairo is a DLL that provides a 2-D graphics library. The pyCairo package provides Python wrappers to allow Python scripts to access the Cairo DLL file. The Cairo DLL file requires two additional DLLs: libpng and zlib. wxCairo is a library that is packaged with wxPython, so don't worry about it until you actually have to start coding.

Download the following packages:

Install everything in this order (feel free not to if you know what you're doing):

  • Double-click on, run python-2.6.2.msi and agree to all defaults. This will install the Python interpreter in C:\Python26\.
  • Double-click on, run  wxPython2.8-win32-unicode-2.8.10.1-py26.exe and agree to all defaults. This will install the wxPython package to C:\Python26\Lib\site-packages\wx-2.8-msw-unicode\
  • Double-click on, run  wxPython2.8-win32-docs-demos-2.8.10.1.exe and agree to all defaults. This will install the docs and demos for wxPython to C:\Program Files\wxPython2.8 Docs and Demos\
  • Double-click on, run  pycairo-1.8.4.win32-py2.6.exe and agree to all defaults. This will install the pyCairo Python wrapper package for Cairo to C:\Python26\Lib\site-packages\cairo\
  • Open cairo_1.8.6-1_win32.zip and extract only bin\libcairo-2.dll to C:\Python26\Lib\site-packages\cairo\libcairo-2.dll (note that the folder in the above path is "Lib", not "libs")
  • Open zlib123-dll.zip and extract only zlib1.dll to C:\Python26\Lib\site-packages\cairo\zlib1.dll
  • Open libpng_1.2.34-1_win32.zip and extract only bin\libpng12-0.dll to C:\Python26\Lib\site-packages\cairo\libpng12-0.dll

Your C:\Python26\Lib\site-packages\cairo folder should now contain the following files:

  • __init__.py
  • __init__.pyc
  • __init__.pyo
  • _cairo.pyd
  • libcairo-2.dll
  • libpng12-0.dll
  • zlib1.dll

Change your computer's PATH variable so that pyCairo can easily find the DLLs that you just installed in its own folder:

  • Get yourself to the PATH variable editing box. Right click on "My computer" -> Properties -> Advanced tab -> Environment Variables button -> Select Path in the System Variables selector box -> Click the Edit button
  • Add the locations of the Python 2.6 executable (not necessary for Cairo, but this makes it a lot easier to run Python scripts from the command line) and the Cairo DLLs (so Cairo will work) to the Windows Path variable list by appending (adding to the end of the existing list) the following to the list (don't forget to separate everything with a semi-colon)
    ;C:\Python26\python.exe;C:\Python26\Lib\site-packages\cairo
  • Click OK
  • Click OK

Check to see that you have installed everything correctly by double-clicking on the Cairo demo script to run it:

C:\Program Files\wxPython2.8 Docs and Demos\demo\cairo.py

A very fancy window entitled "RunDemo: Cairo" with shapes, an image, and text should pop up. If so, congratulations! You're done!

Errors
If things don't work, you will probably see a message that says the following when you run cairo.py:

Sorry. This demo requires the Pycairo package, or there is some other unmet dependency.

You will see the same message if you try the Cairo demo from within the wxPython demo program (demo.py).

Cairo Demos
To see more of Cairo in context run the main wxPython demo (C:\Program Files\wxPython2.8 Docs and Demos\demo\demo.py) and select Cairo or Cairo Snippets under the Miscellaneous section in the right sidebar that pops up.

I hope this has helped you to get started with Cairo!

Tags:

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.