Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the different options for interfacing C (or C++) with Python?

I know there are many ways to interface C function into Python: the Python C API, scipy.weave, ctypes, pyrex/cython, SWIG, Boost.Python, Psyco... What are each of them best for? Why should I use a given method instead of others? What should be considered when I need to choose a binding between Python and C?

I know some discussions about that, but they all seems incomplete...

  • http://wiki.cython.org/SWIG
  • http://sage.math.washington.edu/tmp/sage-2.8.12.alpha0/doc/prog/node35.html

I know that some questions on StackOverflow are related too. For example:

  • About interfacing an existing C library
  • C API vs Cython
like image 311
Charles Brunet Avatar asked Jul 05 '11 18:07

Charles Brunet


People also ask

Can Python and C work together?

Extending Python with C or C++ It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

What is C file in Python?

In this example the C file is self explanatory - it contains two functions, one to add two integers and another to add two floats. In the python file, first the ctypes module is imported. Then the CDLL function of the ctypes module is used to load the shared lib file we created.

Can we import C files in Python?

Perhaps the subprocess module can help. In addition to the question linked in the previous comment (which is about calling C functions from Python), check out Calling an external command in Python. You may be able to compile our C code normally into a program and call that program from Python.


1 Answers

I haven't used all these methods although I have investigated them all at one point or another...

The Python C API: For writing C code that compiles to a python module that can be imported in Python. Or for writing a Python module that acts as "glue" code to interface with some C library.

scipy.weave: Allows you to shove bits of C code into your python code, if you're using NumPy and SciPy for doing numeric work, look into this. The C code would be as a string, like, weave.inline('printf("%s", foo)') for example.

ctypes: A python module that allows you to call in to C code from your python code. You basically import the shared library then make calls into its API. Some work needed to marshall data in and out of those calls. If you're looking at using an existing C library that you or someone else wrote, I'd start here.

pyrex/cython: Allows you to write Python code (using some special syntax) that will get generated into C code (which can be imported as a Python module) and, obviously, run faster than if it was run through the Python interpreter. This is kind of like the "Python C API" route, only it generates the C code for you. Useful if you have some chunk of code that is your bottleneck and is really slow. Rewrite that function using cython and import it from the calling code.

SWIG: Generates wrapper code for a C/C++ library. You should end up with a python module you can import and use.

Boost.Python: This is the one I know the least about. Looks to me like it's similar to SWIG although you write the wrapper layer yourself, but with a lot of help from Boost macros/functions.

Psyco: Speeds up your python code a bit, I've never had much luck with this. I wouldn't waste your time with it. Profile your code, find your bottlenecks and speed them up using one of the above techniques.

like image 114
dgrant Avatar answered Oct 20 '22 11:10

dgrant