Note that there are some explanatory texts on larger screens.

plurals
  1. POWrapping a C library in Python: C, Cython or ctypes?
    primarykey
    data
    text
    <p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning extension writing.</li> <li>Use <a href="http://www.cython.org/" rel="noreferrer">Cython</a> to expose the relevant parts from the C library to Python.</li> <li>Do the whole thing in Python, using <a href="http://docs.python.org/library/ctypes.html" rel="noreferrer"><code>ctypes</code></a> to communicate with the external library.</li> </ol> <p>I'm not sure whether 2) or 3) is the better choice. The advantage of 3) is that <code>ctypes</code> is part of the standard library, and the resulting code would be pure Python &ndash; although I'm not sure how big that advantage actually is.</p> <p>Are there more advantages / disadvantages with either choice? Which approach do you recommend?</p> <hr> <p><strong>Edit:</strong> Thanks for all your answers, they provide a good resource for anyone looking to do something similar. The decision, of course, is still to be made for the single case&mdash;there's no one "This is the right thing" sort of answer. For my own case, I'll probably go with ctypes, but I'm also looking forward to trying out Cython in some other project.</p> <p>With there being no single true answer, accepting one is somewhat arbitrary; I chose FogleBird's answer as it provides some good insight into ctypes and it currently also is the highest-voted answer. However, I suggest to read all the answers to get a good overview.</p> <p>Thanks again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload