Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python C interoperability

Tags:

python

c

interop

I wish to wrap an existing C (pure C that is. No C++) library into Python so that I can call it from Python scripts. Which approach among the various available (C Api, SWIG etc.) would be the most suitable?

like image 895
Frederick The Fool Avatar asked Dec 18 '22 03:12

Frederick The Fool


1 Answers

go with Ctypes, it is part of standard distribution and works very well. basically you can wrap C structures and types in python classes, as well as functions. Some types and functionality is already provided by library.

ctypes

couple caveats though: passing triple pointers to C routines is not obvious (if you have to), and I could not get it to work with static libraries on Linux, DLL and shared objects are fine.

like image 136
Anycorn Avatar answered Dec 31 '22 01:12

Anycorn