开发者

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

开发者 https://www.devze.com 2023-03-17 06:10 出处:网络
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 giv

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:


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消