Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 3 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

CFFI

Use C libraries from Python code.

Languages, types and runtimesPYPI

GitHub

1. License

“Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction”

MIT No Attribution allows any use, including selling, with no notice required.

github.com/python-cffi/cffi/blob/main/LICENSE

From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error

What CFFI does

CFFI is a free Python library that lets Python programs use code written in C. You copy a few C declarations, and Python can call a library already on the computer or a small fast function you wrote in C. It works without a C compiler, but with one it is safer and faster.

What people build with it

Use a C library already on the machine

Your Python app calls a C library that is installed on the computer, such as zlib, without writing any C and without a compiler.

Speed up one slow calculation

A loop that is too slow in Python, like counting or scoring many items, moves into a small C function your Python code calls like any other.

Wrap C source code you were given

You have the .c and .h files of a library, for example from a device maker, and turn them into one module your Python app imports.

Read binary files with a fixed layout

Load pixel data or records from a binary file into C-shaped structures, change a few fields and write them back.

Let a C program run Python code

Package Python code as a plug-in library that an existing C or C++ program loads and calls.

Drive a device from a web backend Idea

A Python backend controls a label printer or scanner through the maker's C library, so the web page can print or scan.

CFFI popularity

65 / 100
Gid Score, #678 overall
298M
downloads a week, pypi
292
GitHub stars
39
agent projects use it
CFFI: downloads per weekPyPI, cffi 296,596,609
CFFI: GitHub starspython-cffi/cffi 292

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How CFFI is used

  • CFFI is a dependency in 39 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (2.4% of the 1,623 Python ones), #9 of 70 in languages, types and runtimes.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,041; those are left out of every share on this page.
  • The most used alternative in our scan is Cython (4 projects).
  • Projects that use it most often also use pycparser (97.4%), Pydantic (92.3%), Click (87.2%).

By coding agent

25 projects, 0.8%
22 projects, 0.7%
11 projects, 0.5%

Often used with

In 97.4% of its projects
In 92.3% of its projects
In 87.2% of its projects
In 87.2% of its projects
In 84.6% of its projects

More about CFFI

More in Languages, types and runtimes

Alternatives

CFFI This page

MIT-0Gid 65298M/wk

pybind11

Pick pybind11 when the code you want to call from Python is C++, which CFFI does not support.

Cython

Pick Cython when you want to speed up existing Python code by compiling it, with C types added where needed.

Apache-2.0Gid 6923.8M/wk

Numba

Pick Numba when the slow part is a numeric loop and you would rather not write any C.

Compare CFFI with another tool

Get started

Tell your AI assistant

“Use cffi (pin cffi==2.1.1, Python 3.10+) to call this C library from Python; prefer API mode with set_source() and a build script, add setuptools on Python 3.12+, pass bytes for char * arguments, and keep every ffi.new() object alive while C uses it.”

  1. Install it into your project's Python environment (Python 3.10 or newer).

    pip install cffi

    From cffi.readthedocs.io/en/stable/installation.html

  2. Declare the C functions you need and load a library that is already installed (no compiler needed).

    >>> from cffi import FFI
    >>> ffi = FFI()
    >>> ffi.cdef("""
    ...     int printf(const char *format, ...);   // copy-pasted from the man page
    ... """)
    >>> C = ffi.dlopen(None)                     # loads the entire C namespace
    >>> arg = ffi.new("char[]", b"world")        # equivalent to C code: char arg[] = "world";
    >>> C.printf(b"hi there, %s.\n", arg)        # call printf
    hi there, world.
    

    From cffi.readthedocs.io/en/stable/overview.html

  3. When a C compiler is available, prefer the compiled mode with set_source() and compile(); it is safer and faster.

    From cffi.readthedocs.io/en/stable/overview.html

Mistakes to avoid

pip install cffi only, then ffibuilder.compile() on Python 3.12+
Add setuptools to the project (pip install setuptools) next to cffi

In our run on Python 3.12 compile() stopped with "This CFFI feature requires setuptools on Python >= 3.12"; it worked after installing setuptools.

C.printf("hi %s", "world") with Python str arguments
C.printf(b"hi %s", ffi.new("char[]", b"world")) or "text".encode("utf-8")

char * parameters take bytes, not str.

p = ffi.new("char **", ffi.new("char[]", b"hello"))
s = ffi.new("char[]", b"hello"); p = ffi.new("char **", s) # keep s in a variable while C uses it

The inner object has no Python reference, so its memory is freed at once and C reads garbage.

ffi.new("char *") to make a text buffer
ffi.new("char[]", b"text") or ffi.new("char[]", size) for a buffer

char * allocates a single character, not a string.

ffi.dlopen(None) to reach the C standard library on Windows
Open a specific DLL by name, e.g. ffi.dlopen("path.dll"), or use the compiled mode

On Python 3 under Windows dlopen(None) does not work.

Questions

Do I need a C compiler?

Not for the simple mode that loads an installed library. The compiled mode, which the docs recommend, needs one on the machine that builds the module.

Why does compile() fail with "requires setuptools" on Python 3.12?

Python 3.12 removed the old build tools CFFI used. Add setuptools to your project; CFFI does not pull it in by itself.

Can I pass a normal Python string to a C function?

No. Functions that take char * expect bytes, so encode the string first.

Can I use it in a commercial product?

Yes. The MIT No Attribution license allows any use, without a credit requirement.

Is it still maintained?

Yes. Version 2.1.1 came out in August 2026 and keeps up with new Python versions.