Packaging Python C Extensions
Once we have a working C extension module, we might want to let other people get it and use it. We need to prepare some information about the module and then prepare it to be distributed.
We will use PEP517, the current standard to package and distribute Python modules.
Information about the module
We need to create a few files, starting by pyproject.toml
that
specifies the tools used for building the package:
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
Then setup.cfg
with information about the module:
[metadata]
name = demo
version = 0.1
author = John Smith
author_email = john@example.com
description = demo is a simple C extension
long_description = file: README.md
long_description_content_type = text/markdown
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: GPLv3
Operating System :: OS Independent
We create README.md
that is mentioned above:
# Demo
The demo module
Create setup.py
with information about the extension, like
C language source files and C libraries that are needed:
from distutils.core import setup, Extension
module = Extension('demo',
sources = ['demo.c'],
libraries = ['asound'])
setup(ext_modules=[module])
Building
We use build
[1]
to make two distribution files: A source distribution [2]
named demo-0.1.tar.gz
that requires compiling on the target machine to create the module, and
a wheel [3]
named demo-0.1-cp39-cp39-linux_i686.whl
, which is a binary distribution that does not required a compiler in the target machine.
This is the command to run build
and the main steps of the output:
$ env/bin/python -m build
* Getting dependencies for sdist...
* Building sdist...
* Building wheel from sdist
* Getting dependencies for wheel...
* Building wheel...
Successfully built demo-0.1.tar.gz and demo-0.1-cp39-cp39-linux_i686.whl
Click here to see the full output.
Installing
References
- build A simple, correct PEP 517 package builder.
- Source Distribution, Glossary in the Python Packaging User Guide
- Wheel, Glossary in the Python Packaging User Guide