all Python 3ctypesterm
ctypes
module
Calling functions in C libraries and working with C types directly from Python.
Syntax
import ctypes
Use it to call an existing C library (DLL, .so) without writing an extension: load it with CDLL, declare argument types and call it. Large bindings are easier with cffi or C extensions, and packing numbers into bytes only needs struct. A wrong argument type is not caught by any check and can crash the whole interpreter; the module is optional and may be missing from a build.
Examples
import importlib.util
if importlib.util.find_spec('ctypes') is not None:
import ctypes
n = ctypes.c_int(41)
n.value += 1
print(n.value) # → ? 42, если ctypes есть в сборке
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy