{
 "kind": "tool",
 "slug": "cffi",
 "path": "tools/cffi/",
 "name": "CFFI",
 "maker": null,
 "density": "full",
 "crumbs": [
  [
   "Tools",
   "tools/"
  ],
  [
   "Languages, types and runtimes",
   "tools/languages-types/"
  ]
 ],
 "lead": "Use C libraries from Python code.",
 "chips": [
  {
   "label": "Languages, types and runtimes",
   "path": "tools/languages-types/"
  },
  {
   "label": "PYPI"
  }
 ],
 "primary": {
  "label": "Copy install command",
  "copy": "pip install cffi"
 },
 "links": [
  {
   "label": "GitHub",
   "href": "https://github.com/python-cffi/cffi"
  }
 ],
 "track": "New versions, deprecation and license changes on your dashboard",
 "verdicts": [
  {
   "key": "license",
   "label": "License",
   "value": "MIT-0",
   "status": "yes",
   "sub": "Use it in paid apps",
   "quote": "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",
   "note": "MIT No Attribution allows any use, including selling, with no notice required.",
   "src": "https://github.com/python-cffi/cffi/blob/main/LICENSE"
  },
  {
   "key": "maint",
   "label": "Maintained",
   "value": "Maintained",
   "status": "yes",
   "sub": "Last change Nov 18, 2026",
   "note": "Latest GitHub release Aug 3, 2026. ",
   "src": "https://github.com/python-cffi/cffi"
  },
  {
   "key": "pop",
   "label": "Used by agents' projects",
   "value": "39 projects",
   "status": "info",
   "sub": "#9 of 70 in its category",
   "note": "Dependency in 39 public GitHub projects set up for AI coding agents that we scanned (0.6%).",
   "src": null
  },
  {
   "key": "agent",
   "label": "Agent access",
   "value": "Install only",
   "status": "info",
   "sub": null,
   "note": "Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.",
   "src": null
  }
 ],
 "vfoot": "From the package registry and GitHub, checked daily; card checked Nov 11, 2026.",
 "blocks": [
  {
   "id": "overview",
   "nav": "Overview",
   "h2": "What CFFI does",
   "parts": [
    {
     "t": "prose",
     "text": "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."
    },
    {
     "t": "h3",
     "text": "What people build with it"
    },
    {
     "t": "uses",
     "items": [
      {
       "title": "Use a C library already on the machine",
       "text": "Your Python app calls a C library that is installed on the computer, such as zlib, without writing any C and without a compiler.",
       "idea": false
      },
      {
       "title": "Speed up one slow calculation",
       "text": "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.",
       "idea": false
      },
      {
       "title": "Wrap C source code you were given",
       "text": "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.",
       "idea": false
      },
      {
       "title": "Read binary files with a fixed layout",
       "text": "Load pixel data or records from a binary file into C-shaped structures, change a few fields and write them back.",
       "idea": false
      },
      {
       "title": "Let a C program run Python code",
       "text": "Package Python code as a plug-in library that an existing C or C++ program loads and calls.",
       "idea": false
      },
      {
       "title": "Drive a device from a web backend",
       "text": "A Python backend controls a label printer or scanner through the maker's C library, so the web page can print or scan.",
       "idea": true
      }
     ]
    }
   ]
  },
  {
   "id": "popularity",
   "nav": "Popularity",
   "h2": "CFFI popularity",
   "parts": [
    {
     "t": "stats",
     "items": [
      {
       "n": "65",
       "of": "/ 100",
       "l": "Gid Score, #678 overall"
      },
      {
       "n": "298M",
       "l": "downloads a week, pypi"
      },
      {
       "n": "292",
       "l": "GitHub stars"
      },
      {
       "n": "39",
       "l": "agent projects use it"
      }
     ]
    },
    {
     "t": "small",
     "text": "Gid Score measures public popularity and activity, not quality.",
     "link": "How the Gid Score is calculated",
     "path": "tools/top/"
    }
   ]
  },
  {
   "id": "usage",
   "nav": "How it is used",
   "h2": "How CFFI is used",
   "parts": [
    {
     "t": "list",
     "items": [
      "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%)."
     ]
    },
    {
     "t": "h3",
     "text": "By coding agent"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "25 projects, 0.8%",
       "v": "GitHub Copilot",
       "path": "agents/github-copilot/"
      },
      {
       "l": "22 projects, 0.7%",
       "v": "Claude Code",
       "path": "agents/claude-code/"
      },
      {
       "l": "11 projects, 0.5%",
       "v": "Cursor",
       "path": "agents/cursor/"
      }
     ]
    },
    {
     "t": "h3",
     "text": "Often used with"
    },
    {
     "t": "rows",
     "rows": [
      {
       "l": "In 97.4% of its projects",
       "v": "pycparser",
       "path": "tools/pycparser/"
      },
      {
       "l": "In 92.3% of its projects",
       "v": "Pydantic",
       "path": "tools/pydantic/"
      },
      {
       "l": "In 87.2% of its projects",
       "v": "Click",
       "path": "tools/click-py/"
      },
      {
       "l": "In 87.2% of its projects",
       "v": "urllib3",
       "path": "tools/urllib3/"
      },
      {
       "l": "In 84.6% of its projects",
       "v": "cryptography",
       "path": "tools/cryptography/"
      }
     ]
    }
   ]
  },
  {
   "id": "more",
   "nav": null,
   "spy_as": "usage",
   "h2": "More about CFFI",
   "parts": [
    {
     "t": "more",
     "items": [
      {
       "title": "More in Languages, types and runtimes",
       "path": "tools/languages-types/",
       "items": [
        {
         "text": "TypeScript",
         "path": "tools/typescript/"
        },
        {
         "text": "tsx",
         "path": "tools/tsx/"
        },
        {
         "text": "ts-node",
         "path": "tools/ts-node/"
        },
        {
         "text": "jiti",
         "path": "tools/jiti/"
        },
        {
         "text": "vue-tsc",
         "path": "tools/vue-tsc/"
        },
        {
         "text": "Cloudflare Workers Types",
         "path": "tools/cloudflare-workers-types/"
        },
        {
         "text": "mypy",
         "path": "tools/mypy/"
        },
        {
         "text": "tsconfig-paths",
         "path": "tools/tsconfig-paths/"
        }
       ]
      }
     ]
    }
   ]
  },
  {
   "id": "alternatives",
   "nav": "Alternatives",
   "h2": "Alternatives",
   "parts": [
    {
     "t": "alts",
     "items": [
      {
       "name": "CFFI",
       "me": true,
       "facts": [
        "MIT-0",
        "Gid 65",
        "298M/wk"
       ]
      },
      {
       "name": "pybind11",
       "why": "Pick pybind11 when the code you want to call from Python is C++, which CFFI does not support.",
       "path": "tools/pybind11/",
       "facts": []
      },
      {
       "name": "Cython",
       "why": "Pick Cython when you want to speed up existing Python code by compiling it, with C types added where needed.",
       "path": "tools/cython/",
       "facts": [
        "Apache-2.0",
        "Gid 69",
        "23.8M/wk"
       ]
      },
      {
       "name": "Numba",
       "why": "Pick Numba when the slow part is a numeric loop and you would rather not write any C.",
       "path": "tools/numba/",
       "facts": []
      }
     ],
     "more": "Compare CFFI with another tool",
     "more_path": "tools/compare/"
    }
   ]
  },
  {
   "id": "start",
   "nav": "Get started",
   "h2": "Get started",
   "parts": [
    {
     "t": "prompt",
     "text": "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."
    },
    {
     "t": "steps",
     "items": [
      {
       "text": "Install it into your project's Python environment (Python 3.10 or newer).",
       "code": "pip install cffi",
       "src": "https://cffi.readthedocs.io/en/stable/installation.html"
      },
      {
       "text": "Declare the C functions you need and load a library that is already installed (no compiler needed).",
       "code": ">>> from cffi import FFI\n>>> ffi = FFI()\n>>> ffi.cdef(\"\"\"\n...     int printf(const char *format, ...);   // copy-pasted from the man page\n... \"\"\")\n>>> C = ffi.dlopen(None)                     # loads the entire C namespace\n>>> arg = ffi.new(\"char[]\", b\"world\")        # equivalent to C code: char arg[] = \"world\";\n>>> C.printf(b\"hi there, %s.\\n\", arg)        # call printf\nhi there, world.\n",
       "src": "https://cffi.readthedocs.io/en/stable/overview.html"
      },
      {
       "text": "When a C compiler is available, prefer the compiled mode with set_source() and compile(); it is safer and faster.",
       "code": null,
       "src": "https://cffi.readthedocs.io/en/stable/overview.html"
      }
     ]
    },
    {
     "t": "h3",
     "text": "Mistakes to avoid"
    },
    {
     "t": "mistakes",
     "items": [
      {
       "dont": "pip install cffi only, then ffibuilder.compile() on Python 3.12+",
       "fix": "Add setuptools to the project (pip install setuptools) next to cffi",
       "why": "In our run on Python 3.12 compile() stopped with \"This CFFI feature requires setuptools on Python >= 3.12\"; it worked after installing setuptools."
      },
      {
       "dont": "C.printf(\"hi %s\", \"world\") with Python str arguments",
       "fix": "C.printf(b\"hi %s\", ffi.new(\"char[]\", b\"world\")) or \"text\".encode(\"utf-8\")",
       "why": "char * parameters take bytes, not str."
      },
      {
       "dont": "p = ffi.new(\"char **\", ffi.new(\"char[]\", b\"hello\"))",
       "fix": "s = ffi.new(\"char[]\", b\"hello\"); p = ffi.new(\"char **\", s)  # keep s in a variable while C uses it",
       "why": "The inner object has no Python reference, so its memory is freed at once and C reads garbage."
      },
      {
       "dont": "ffi.new(\"char *\") to make a text buffer",
       "fix": "ffi.new(\"char[]\", b\"text\") or ffi.new(\"char[]\", size) for a buffer",
       "why": "char * allocates a single character, not a string."
      },
      {
       "dont": "ffi.dlopen(None) to reach the C standard library on Windows",
       "fix": "Open a specific DLL by name, e.g. ffi.dlopen(\"path.dll\"), or use the compiled mode",
       "why": "On Python 3 under Windows dlopen(None) does not work."
      }
     ]
    }
   ]
  },
  {
   "id": "faq",
   "nav": "Questions",
   "h2": "Questions",
   "parts": [
    {
     "t": "faq",
     "items": [
      {
       "q": "Do I need a C compiler?",
       "a": "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.",
       "evidence": {
        "url": "https://cffi.readthedocs.io/en/stable/overview.html",
        "quote": "The ABI mode accesses libraries at the binary level, whereas the faster API mode accesses them with a C compiler."
       }
      },
      {
       "q": "Why does compile() fail with \"requires setuptools\" on Python 3.12?",
       "a": "Python 3.12 removed the old build tools CFFI used. Add setuptools to your project; CFFI does not pull it in by itself.",
       "evidence": {
        "url": "https://cffi.readthedocs.io/en/stable/whatsnew.html",
        "quote": "CFFI does not declare a runtime setuptools requirement to avoid an unnecessary dependency for projects that do not require it."
       }
      },
      {
       "q": "Can I pass a normal Python string to a C function?",
       "a": "No. Functions that take char * expect bytes, so encode the string first.",
       "evidence": {
        "url": "https://cffi.readthedocs.io/en/stable/overview.html",
        "quote": "Note that char * arguments expect a bytes object."
       }
      },
      {
       "q": "Can I use it in a commercial product?",
       "a": "Yes. The MIT No Attribution license allows any use, without a credit requirement.",
       "evidence": {
        "url": "https://github.com/python-cffi/cffi/blob/main/LICENSE",
        "quote": "MIT No Attribution"
       }
      },
      {
       "q": "Is it still maintained?",
       "a": "Yes. Version 2.1.1 came out in August 2026 and keeps up with new Python versions.",
       "evidence": {
        "url": "https://cffi.readthedocs.io/en/stable/whatsnew.html",
        "quote": "Avoids breaking ABI change in Python >= 3.15.0b4."
       }
      }
     ]
    }
   ]
  }
 ],
 "glance": [
  [
   "Package",
   "pypi cffi"
  ],
  [
   "Category",
   "Languages, types and runtimes"
  ],
  [
   "License",
   "MIT-0"
  ],
  [
   "Gid Score",
   "64.9"
  ],
  [
   "Rank in category",
   "#9 of 70"
  ],
  [
   "GitHub",
   "python-cffi/cffi"
  ],
  [
   "Last change",
   "Nov 18, 2026"
  ]
 ],
 "good": [
  "It needs Python 3.10 or newer; the docs list Python 3.10 to 3.15 as tested.",
  "Without a compiler, a small mistake in the declarations makes the program crash instead of showing an error.",
  "It works with C only, not C++; for C++ code you add a small C layer or use another tool."
 ],
 "checked_line": "Read by AgentGid from 9 official pages, card checked Nov 11, 2026; numbers update daily.",
 "logo": "https://agentgid.com/media/avatars/python-cffi.png"
}