cairocffi provides Python bindings for the Cairo 2D graphics library, built using CFFI. It aims for API compatibility with Pycairo, allowing Python developers to leverage Cairo's powerful graphics capabilities with a more modern CFFI-based backend. The current version is 1.7.1, with releases occurring infrequently, often driven by new Cairo versions or minor bug fixes.
pip install cairocffiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an image surface, draw text onto it, and save the result as a PNG file. It highlights the typical import pattern and basic drawing operations using the Cairo API.
If your code expects a `bytearray`, convert the `memoryview` explicitly: `bytearray(surface.get_data())`. Using `memoryview` directly is generally more efficient.
cairocffi binds to the native Cairo library. On Debian/Ubuntu, install `libcairo2-dev`. On Fedora, `cairo-devel`. On macOS with Homebrew, `brew install cairo`.
Always call `surface.finish()` on surfaces that write to files or other streams when you are done with them to prevent incomplete or corrupted output. For `ImageSurface`, it's less critical as data is held in memory, but still good practice.
Always use `import cairocffi as cairo` to maintain API compatibility while using the correct module import path.
Install the Cairo development libraries for your operating system. For Debian/Ubuntu: `sudo apt-get install libcairo2-dev`. For Fedora/RHEL: `sudo dnf install cairo-devel`. For macOS with Homebrew: `brew install cairo`. For Windows, install a GTK+ distribution (like MSYS2 or GTK+ for Windows Runtime Installer) which includes `libcairo-2.dll` and ensure its directory is in your system's PATH environment variable or set `CAIROCFFI_DLL_DIRECTORIES`.
Install `cairocffi` using pip, ensuring you use the correct `pip` executable for your Python environment (e.g., `pip3 install cairocffi` if using Python 3).
Rename your local `constants.py` file to avoid the naming conflict, or ensure that `cairocffi` is installed and run in an isolated virtual environment where such conflicts are less likely.
Upgrade `cairocffi` to its latest version: `pip install --upgrade cairocffi`. If the issue persists, try reinstalling it in a clean virtual environment.