0

I'm following the python documentation about "distributing packages using setuptools". It doesn't mention what to do if a python package you want to include in your install_requires requires a system installation. For example:

I'd like to include the package pysoundfile, but this requires me to install libsndfile1 via my system package manager, e.g. sudo apt-get install libsndfile1.

I've got as far as understanding I need to make a binary distribution, given advice in the docs here, but that sends me to the docs listed above.

How do I automate this process such that users of my package can simply pip install my_package and not have to apt-get install libsndfile1?

1

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
1
  • This is incredibly helpful. Thank you. – James Owers Apr 21 '20 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.