17

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...

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

Community
  • 1
  • 1
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • 2
    -1. This question is overly broad. – Steven Rumbalski Jul 05 '11 at 19:00
  • 1
    @Steven Rumbalski: I disagree. This is a good question, although I suppose I'd love to hear what the user has actually *tried*. – Platinum Azure Jul 05 '11 at 19:02
  • @Platinum Azure: I waffled a little on the -1, but ultimately decided that a good answer to this question would have to be really long and detailed. It would also be largely subjective. – Steven Rumbalski Jul 05 '11 at 19:10
  • @Steven Rumbalski: The question is faceted enough where at least mostly objective answers are possible. "What are each of them best for?" can be answered objectively (in theory). "What should be considered when I need to choose a binding between Python and C?" requires a non-trivial answer but could also be answered objectively. – Platinum Azure Jul 05 '11 at 19:12
  • 1
    @Steven Rumbalski, I agree -- this strikes me as a slightly narrower version of the "what language should I learn next" question. @Charles Brunet, I think you should edit this to reflect your specific needs. What do you want to do? There are simply too many options to give a good answer to this question as-is. – senderle Jul 05 '11 at 19:18
  • 1
    I'd disagree. If you know you need a c binding to python, but you don't know enough to discern what it is about the binding you need to look at, then how can you ask a more specific question? – Spencer Rathbun Jul 05 '11 at 20:37

2 Answers2

9

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.

dgrant
  • 1,417
  • 3
  • 16
  • 23
4

This is only a brief answer to a portion of your question, but:

ctypes is probably best when you have a preexisting C library that you want to use with Python.

The Python C API is best when you either want to write something in C that utilizes aspects of Python, or want to write an extension for Python in C. (Cython is another way of doing this.)

Of course, both of those are likely elaborated on in much more detail in some of the answers to the SO questions you link to in your question.

JAB
  • 20,783
  • 6
  • 71
  • 80
  • I've attempted both SWIG and ctypes recently, and ctypes turned out to be way less of a headache. You can just compile your C/C++ as a shared library, then import and call the functions from a Python script. – Chriszuma Jul 05 '11 at 20:56
  • but, `ctypes` is not able to create C++ class object easily. – 陈家胜 Jan 28 '18 at 08:33
  • @陈家胜 True enough, which is where dgrant's answer comes in. (Though it's always possible to provide a C-interface wrapper for the C++ parts.) – JAB Jan 28 '18 at 17:42