Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to package a Python library that includes a C shared library?

I have written a library whose main functionality is implemented in C (speed is critical), with a thin Python layer around it to deal with the ctypes nastiness.

I'm coming to package it and I'm wondering how I might best go about this. The code it must interface with is a shared library. I have a Makefile which builds the C code and creates the .so file, but I don't know how I compile this via distutils. Should I just call out to make with subprocess by overriding the install command (if so, is install the place for this, or is build more appropriate?)

Update: I want to note that this is not a Python extension. That is, the C library contains no code to itself interact with the Python runtime. Python is making foreign function calls to a straight C shared library.

like image 686
obeattie Avatar asked Jun 05 '14 14:06

obeattie


2 Answers

Given that you followed the instructions on how to create Python extensions in C, you should just enlist the extension modules like in this documentation.
So the setup.py script of your library should look like this:

from distutils.core import setup, Extension
setup(
   name='your_python_library',
   version='1.0',
   ext_modules=[Extension('your_c_extension', ['your_c_extension.c'])],
)

and distutils knows how to compile your extension to C shared library and moreover where to put it.

Of course I have no further information about your library, so you probably want to add more arguments to setup(...) call.

like image 56
ElmoVanKielmo Avatar answered Oct 15 '22 04:10

ElmoVanKielmo


I'd consider building the python module as a subproject of a normal shared library build. So, use automake, autoconf or something like that to build the shared library, have a python_bindings directory with a setup.py and your python module.

like image 32
Sam Hartman Avatar answered Oct 15 '22 03:10

Sam Hartman