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

I am writing some bindings for the C library and don’t know how to configure all this for distribution so that pip install my package.

Let's say I have the following files:

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

For my wrapper library to work, you must:

  • compile library.c and create a shared library
  • run ctypesgen on library.h to generate 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 who installed ctypesgen .

I have no idea how to configure all this so that someone who is interested in the library can just pip install library and all this happens automatically. Who can help?

+7
python setuptools distutils ctypes
source share
1 answer

The setuptools / distutils extension options are what you need.

The documentation contains additional information , in a nutshell setup.py example, to do the above will look 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, ) 

When you build the .so module, setup.py is automatically created. If it needs to link to other libraries, you can provide a list of libraries arguments for extension. See the documents ( 1 ) for more details.

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

If you want to create distribution binaries, including native code, see manylinux .

+2
source share

All Articles