A quick example of Python 3.10 modules implemented in C using the Python/C API.
make all
python -c "import hello; hello.greet('World')"
python -c "import fib; print(fib.fib(5))"
make clean
Note that the best way to distribute a Python module is to use a setup.py
file. The Makefile is here only to debunk Python's magic.
- Write a
foobarmodule.c
C file. Note the#include <Python.h>
at the beginning of example files. - Compile
foobarmodule.c
and produce afoobar.so
file. See the Makefile for details. - Put
foobar.so
in a folder listed in the Python path. - Load the module from Python with
import foobar
.