I am attempting to run some matplotlib tutorials on my Pi3 running Raspbian Stretch. I have installed matplotlib & numpy using pip3

When trying to run any of the tutorials I get an error ImportError: No module named 'cairocffi' and ImportError: Cairo backend requires that cairocffi or pycairo is installed.

I attempted to install cairocffi

Collecting cairocffi
  Using cached https://www.piwheels.hostedpi.com/simple/cairocffi/cairocffi-0.8.0-py3-none-any.whl
Collecting cffi>=1.1.0 (from cairocffi)
  Using cached cffi-1.11.2.tar.gz
Collecting pycparser (from cffi>=1.1.0->cairocffi)
  Using cached https://www.piwheels.hostedpi.com/simple/pycparser/pycparser-2.18-py2.py3-none-any.whl
Building wheels for collected packages: cffi
  Running setup.py bdist_wheel for cffi: started
  Running setup.py bdist_wheel for cffi: finished with status 'error'
  Package libffi was not found in the pkg-config search path.
  Perhaps you should add the directory containing `libffi.pc'
  to the PKG_CONFIG_PATH environment variable
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-armv7l-3.5
  creating build/lib.linux-armv7l-3.5/cffi
  building '_cffi_backend' extension
  creating build/temp.linux-armv7l-3.5
  creating build/temp.linux-armv7l-3.5/c
  arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fdebug-prefix-map=/build/python3.5-RUbMX3/python3.5-3.5.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/usr/include/python3.5m -c c/_cffi_backend.c -o build/temp.linux-armv7l-3.5/c/_cffi_backend.o
  c/_cffi_backend.c:15:17: fatal error: ffi.h: No such file or directory
   #include <ffi.h>
  compilation terminated.
  error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

  Running setup.py clean for cffi
Failed to build cffi
Installing collected packages: pycparser, cffi, cairocffi
  Running setup.py install for cffi: started
    Running setup.py install for cffi: finished with status 'error'
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-3l0i4pgl/cffi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-hlec49cb-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable

The tutorials apparently run successfully on Windows and other machines, so I am assuming the problem is a Pi specific issue, and not just a python problem.

Can anyone point me in the right direction. I note /usr/lib/arm-linux-gnueabihf/libffi.so.6 is on the Pi.


1 Answer 1


Try this: install libffi6 and libffi-dev using apt:

 sudo apt install libffi-dev libffi6

Then rerun the pip install of cairocffi:

pip3 install cairocffi

You may also need to install cffi (but I don't think so):

pip3 install cffi
  • Thanks, this fixed the problem. I now get a graphics window. It doesn´t actually display the expected output and produces errorypeError: Couldn't find foreign struct converter for 'cairo.Context'`, but this is a new problem.
    – Milliways
    Oct 22, 2017 at 3:16
  • @millways try this sudo apt-get install python-gi-cairo Oct 22, 2017 at 3:43
  • Steve, again thanks for the hint. Did not work but sudo apt install python3-gi-cairo did. <rant>From a theoretical point Python seems good; I have even written a few programs BUT the documentation is non-existent or APPALLING, error messages cryptic! Who would produce a package which requires to compile code, BUT not indicate the need to install dev libraries?</rant> The end result is that whenever I have to produce a program which works, it is easier to write in c or c++ (or indeed ANY language with documentation). I will also give the author of the tutorial my opinion!
    – Milliways
    Oct 22, 2017 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.