Pycairo is a Python module providing bindings for the cairo graphics library. It enables Python programs to create high-quality vector graphics that scale without loss of clarity and can be output in various formats like PNG, SVG, PDF, and PostScript. The library is actively maintained with regular releases, typically multiple per year, following semantic versioning since v1.11.0. It currently supports Python 3.10+ and PyPy3.
pip install pycairoVerified import paths — ran on the pinned version, not inferred.
This quickstart code creates an ImageSurface and a Context object, then draws a Bezier curve with guide lines, and finally saves the output to a PNG file. This demonstrates basic surface and context creation, drawing commands, and outputting to a file.
Upgrade your Python interpreter to 3.10+ or PyPy3.11+ before upgrading Pycairo.
Adjust type expectations or comparisons for the return value of `Path.__iter__()` to account for `PathDataType`.
Ensure `ImageSurface.get_data()` is called only on active surfaces. Avoid calling it after `Surface.finish()` has been invoked or the underlying surface is destroyed.
Install the necessary system dependencies *before* running `pip install pycairo`. For example, on Debian/Ubuntu: `sudo apt install libcairo2-dev pkg-config python3-dev`; on macOS with Homebrew: `brew install cairo pkg-config`.
Design drawing logic to be fully re-executable. Store high-level drawing parameters (e.g., coordinates, colors) in application state and use them to redraw the entire scene whenever a redraw is needed. Do not expect `Context` state to persist indefinitely.
Force `pip` to rebuild and reinstall `pycairo` by running: `pip install --force-reinstall --no-cache-dir pycairo`. This ensures `pycairo` is linked against the currently installed `cairo` library.