TL/DR; Use Python's distutils module in
setup.py in order to bundle
libsndfile together with your application in a single binary Wheel.
Python packages can come as source distributions or as binary distributions (Python Wheels,
.whl). When a package requiring a native library (e.g.
libsndfile) is installed from source, the installing system needs to have the native library installed and a corresponding toolchain (e.g. gcc) to link the Python package with the native library.
To avoid the need for a compile toolchain and for
apt-get install …, you can use Python's distutils module. Especially
distutils.build_clib is what you are looking for. This allows you to build
libsndfile as a static library inside
setup.py and bundle it into a Python Wheel together with your application. You might want to have look at other projects that use this method, such as argon2_cffi.
Note that bundling native dependencies together with your Python application has drawbacks as well:
- Since the native libraries are statically linked, users of your application will not get any security updates when they update their OS version of
libsndfile. You have to take care of that.
- Python Wheels are specific to a platform (i.e. combination of CPU architecture and OS). If you intend to support many different platforms, you need to build many different wheels. Take a look at the many wheels produced by the Pillow project