Extension Modules
The Python/C API defines a process by which Python interoperates with C (and therefore, also with other languages that can be called from C). Extension modules are built from compiled code, communicating with Python through the C API, and which can be imported into Python in the same manner as a module written in pure Python. Writing extension modules by hand is a laborious process, so many tools have been developed to facilitate some of that interface code generation. In addition, other libraries have been developed to facilitate calling foreign functions directly from Python. We highlight a few of the possibilities below.
- CFFI and ctypes provide "foreign function interfaces", or lightweight APIs, for calling C libraries from within Python.
- Boost.python helps write C++ libraries that Python can import and use easily.
- SWIG reads C and C++ header files and generates a library than Python can load.
- F2PY reads Fortran code and generates a library that Python can load.
- PyCUDA and PyOpenCL provide access within Python to GPUs.
- You can also write C code by hand, that can be called by Python.
CFFI and ctypes are probably the most robust means of calling compiled C code from Python, albeit with somewhat narrower scopes than many of the packages listed here. They keep the C and Python worlds separate and self-contained. These are API-based approaches, in which you import a Python package that opens up a C-like interface to compiled C libraries. They are probably best when you need to call some functions from a C/C++ API, although they both require something of an admixture of Python and C constructs that makes code more complicated to read.
Boost.python, SWIG (the Simplified Wrapper and Interface Generator), and F2PY all generate wrapper or interface code that allows for calling compiled code from Python. Boost.python allows interfacing between Python and C++; SWIG generates wrappers for both C and C++ (although not for all of C++); and F2PY facilitates calling Fortran functions from Python. F2PY is part of the NumPy package, and is therefore widely available. These packages all differ in their details, but broadly speaking, they are able to parse the declaration of functions and objects in compiled code, and then generate wrapper code compliant with the Python/C API in such a way that those functions and objects can be accessed from within Python as if they were pure Python functions and objects. The effect is more or less the same as what you would get if you wrote that wrapper code by hand, but hopefully with less tedium and fewer errors. These approaches can be generally useful if you have an existing code base in one of these compiled languages, and would like to be able to access that functionality more flexibly within Python (e.g., to interactively steer a computation, or glue together your application code with other useful Python libraries).