Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I package for distribution a python module that uses a shared library?

I'm writing some bindings for a C library and am not sure how to configure all this for distribution so it is possible to pip install my package.

Let's say I have the following files:

  • library.c
  • library.h
  • wrapper.py

In order for my wrapper library to work it is necessary to:

  • compile library.c and create a shared library
  • run ctypesgen on library.h to generate the ctypes code

Here are the commands:

  • gcc -Wall -fPIC -c library.c
  • gcc -shared -Wl,-soname,liblibrary.so.1 -o liblibrary.so.1.0 library.o
  • ctypesgen.py library.h -L ./ -l library -o _library.py

Running setup.py will also depend on the user having installed ctypesgen.

I have no idea how to get this all set up so that someone interested in the library can simply pip install library and have all this happen automagically. Anyone able to help?

like image 232
coleifer Avatar asked Jun 10 '14 16:06

coleifer


1 Answers

The Extensions capability of setuptools/distutils is what you need.

Documentation has more information, in short an example setup.py to do the above would look like the below

from setuptools import setup, find_packages, Extension

extensions = [Extension("my_package.ext_library",
                       ["src/library.c"],
                       depends=["src/library.h"],
                       include_dirs=["src"],
              ),
]
setup(<..>,
     ext_modules=extensions,
)

The .so is generated automatically by setup.py when the module is built. If it needs to link to other libraries can supply a libraries argument list to the extension. See docs(1) for more info.

Since this is built in functionality of setuptools, it works fine with pip and can be distributed (as source code only) on pypi. All source files referenced by the Extension must be present in the distributed pypi archive.

If you want to build distributable binary wheels including native code see manylinux.

like image 97
danny Avatar answered Sep 19 '22 19:09

danny