r/fortran 5d ago

PRIK – Generate native Python bindings from Fortran (with editable API contracts)

Hi everyone,I’ve been working on an open-source tool called PRIK (Python Runtime Interop Kit) that generates native Python bindings from Fortran (and C) source.

Basic usage:

pip install prik
python3 -m prik my_module.f90 --out my_module

This produces an importable Python extension.

The main idea is that the Python API doesn’t have to mirror the native one. PRIK generates an editable `.pyi` contract that you can modify to:

  • rename procedures
  • turn them into methods
  • hide arguments
  • return native outputs as Python values
  • support overloads and callbacks

…all without changing the original Fortran/C code.

It currently supports modules, derived types (as Python classes), NumPy arrays, strings, allocatables, pointers, optional arguments, generics, callbacks, and more.

I’ve been testing it on real libraries rather than just toy examples:

  • BLAS (155 routines)
  • LAPACK (127 double-precision routines)
  • FFTPACK
  • MINPACK (including callbacks)
  • BSPLINE-FORTRAN (modern Fortran classes)

C support exists but is less mature (tested with libm and TA-Lib). Still alpha, so the API may change. Fortran support is currently much stronger than C.

Docs: https://pynumlab.github.io/prik/

GitHub: https://github.com/PyNumLab/prik

Feedback, bug reports, or “does this work with my code?” experiments are very welcome!

5 Upvotes

0 comments sorted by