The ctypes module is Python's built-in foreign function interface. It lets you load a shared library (a .so, .dylib or .dll), call its C functions, and create C-compatible data such as structs, arrays and callbacks, all from pure Python with no compiler involved.
Use it when you need a few functions from an existing C library or an operating system API. For large or performance-critical bindings, other tools are usually a better fit, and this article compares them.
ctypes.CDLL loads a library that uses the standard C calling convention. On Windows, ctypes.WinDLL loads libraries that use the stdcall convention, which is what most Win32 APIs use. To find a library by its short name in a portable way, use ctypes.util.find_library.
On Windows the standard C runtime is not found as "m"; call Windows libraries directly, for example ctypes.WinDLL("kernel32"). Passing None to CDLL on Linux and macOS gives you symbols already loaded in the current process, including the C library.
By default ctypes assumes every argument is a C int and every return value is an int. That is wrong for doubles and pointers, so always declare the signature.
With argtypes set, ctypes also checks the arguments and raises ctypes.ArgumentError instead of corrupting memory when you pass the wrong type.
| ctypes type | C type | Python value |
|---|---|---|
c_int, c_uint | int, unsigned int | int |
c_long, c_longlong | long, long long | int |
c_size_t | size_t | int |
c_double, c_float | double, float | float |
c_bool | _Bool | bool |
c_char_p | char * | bytes or None |
c_wchar_p | wchar_t * | str or None |
c_void_p | void * | int or None |
Each type is a constructor: ctypes.c_int(5) creates a mutable C integer, and its value is in .value. Use ctypes.byref(x) to pass a pointer to it, which is how out-parameters work.
Subclass ctypes.Structure and list the fields in _fields_. ctypes lays them out the same way a C compiler would on that platform.
Multiplying a type by an integer creates an array type. Use ctypes.Union for unions and _pack_ when the C struct is packed. Set _fields_ only once, before the class is first used.
ctypes.CFUNCTYPE builds a function type that wraps a Python callable so C code can call it. The classic example is sorting with the C library function qsort:
Keep a reference to the callback object for as long as C may call it. If it is garbage collected, the next call from C crashes the interpreter.
ctypes.wintypes provides the Windows type names (DWORD, HANDLE, LPCWSTR), and ctypes.WinError() turns the last error into an OSError. Pass use_last_error=True when loading the DLL and read the code with ctypes.get_last_error().
CDLL and WinDLL release the global interpreter lock while the foreign function runs, so other Python threads can continue. ctypes.PyDLL, and ctypes.pythonapi, keep the GIL held because they call the Python C API.
ctypes has no safety net. A wrong signature, a dangling pointer or a freed buffer can crash the process or corrupt memory, and nothing warns you. It also cannot read C headers, so you rewrite every struct and prototype by hand.
| Tool | Best for |
|---|---|
ctypes | A few calls into an existing library, no build step |
cffi | Larger C APIs, parsing declarations from header text |
pybind11, nanobind | Exposing C++ code to Python |
| Cython | Writing fast extension code in a Python-like language |
Yes. It ships with CPython, so there is nothing to install.
CDLL uses the cdecl calling convention, while WinDLL uses stdcall. On 64-bit Windows the two are the same in practice, but you should still use the one that matches the documentation of the library.
Each call has overhead from argument conversion, so calling a tiny function millions of times in a loop is slow. Pass whole arrays or buffers per call, or use a compiled extension.
Yes. Use ctypes.util.find_library("name") to find it, or pass the full path to CDLL.
ctypes is the quickest way to call C libraries from Python: load the library, declare argtypes and restype, and use Structure, arrays and CFUNCTYPE for richer data. Keep the signatures exact, keep references to anything C may use later, and switch to cffi, pybind11 or Cython when the binding becomes large.