guide 04 / 08
Can I Run C in Python? 5 Ways to Call C Code from CPython
Yes, you can run C code from Python. Compare ctypes, cffi, C extensions, Cython and pybind11, with short CPython examples and when to use each.
TL;DR
- Python can't execute raw C source, but CPython can call compiled C code.
- ctypes, in the standard library, calls functions in existing shared libraries with no compiling.
- C extensions and Cython produce fast native modules you import like normal Python.
- Choose based on whether you're wrapping existing C or writing new fast code.
01The honest answer
You can't paste C source into a .py file and run it. But CPython is built in C and designed to call compiled C code, so in practice the answer is yes. You compile the C into a shared library or an extension module, and Python calls into it. Here are the five main options, from easiest to most powerful.
021. ctypes: call an existing library
ctypes ships with CPython and loads shared libraries (.so, .dll, .dylib) at runtime:
// add.c — compile with: gcc -shared -fPIC -o libadd.so add.c
int add(int a, int b) { return a + b; }
import ctypes
lib = ctypes.CDLL("./libadd.so")
lib.add.argtypes = (ctypes.c_int, ctypes.c_int)
lib.add.restype = ctypes.c_int
print(lib.add(2, 40)) # 42
Good for: quick wrappers around existing libraries. Watch out: wrong type declarations can crash the interpreter.
032. cffi: a friendlier foreign interface
cffi is a third-party package where you paste C declarations as strings. It is popular for wrapping larger C libraries and works on CPython and PyPy.
043. A hand-written C extension
Using the CPython C API directly (#include <Python.h>), you write a module that defines Python functions in C. It's the fastest and most flexible option, but you must handle reference counts and error checking yourself. The official “Extending and Embedding” tutorial in the Python docs walks through it.
054. Cython: Python-like code compiled to C
Cython lets you write code that looks like Python, add optional C types, and compile it into an extension module:
# fast.pyx
def total(int n):
cdef long s = 0
cdef int i
for i in range(n):
s += i
return s
After building, import fast works like any module. It is ideal for speeding up hot loops without leaving Python syntax. See CPython vs Cython.
065. pybind11 or nanobind for C++
If your native code is C++, pybind11 and nanobind create bindings with very little boilerplate. Many scientific libraries use them.
07Which one should you pick?
| Situation | Best choice |
|---|---|
| Call a few functions in an existing .dll/.so | ctypes |
| Wrap a large C library, support PyPy too | cffi |
| Speed up your own slow Python loop | Cython |
| Expose a C++ codebase | pybind11 / nanobind |
| Maximum control, minimal dependencies | Hand-written C extension |
08Safety tips
- Declare argument and return types in ctypes. The defaults assume
intand can silently corrupt data. - Never free memory on the C side that Python still references.
- In hand-written extensions, check every return value and handle reference counts carefully. A debug build of CPython (
--with-pydebug) catches many mistakes early. - Release the GIL only around code that doesn't touch Python objects.
09Performance: when calling C pays off
Every call from Python into C has a small overhead for converting arguments and results. That means:
- Big wins: one call that does a lot of work, such as processing an entire array, a whole image or a large file.
- Small or negative wins: millions of tiny calls that each do almost nothing. Here the conversion cost dominates.
Batch your work so each C call does more, and measure with a profiler before and after. Python 3.15's sampling profiler (PEP 799) and the long-standing cProfile module both help here.
10Learn from the source
CPython's own Modules/ directory is full of real C extension modules, such as _json, _sqlite and math. They are excellent references for writing your own:
??Quick questions
Q01Can Python run C code directly?
Not raw source. You compile the C first, then call it through ctypes, cffi or an extension module.
Q02Is calling C from Python faster?
For heavy computation, yes. For tiny functions, the call overhead can outweigh the gain.