Hi everyone,
I’d like to introduce PRIK (Python Runtime Interop Kit), an open-source project I’ve been working on for generating native Python bindings for Fortran and C code.
The basic usage can be as simple as:
pip install prik
python3 -m prik my_module.f90 --out my_module
which produces an importable Python extension from the Fortran source.
One of the main ideas behind PRIK is that the generated Python API does not have to exactly mirror the native API.
PRIK can generate an editable .pyi contract describing the Python/native boundary:
python3 -m prik generate --pyi my_module.f90 --out contracts
The contract can then be edited to reshape the Python-facing API—for example, to:
- rename procedures
- turn procedures into methods
- hide native arguments
- expose native outputs as Python return values
- expose overloads and callbacks.
The original Fortran source does not need to be modified.
Supported features include modules, derived types (as Python classes), multidimensional arrays with NumPy interop, strings, allocatables, pointers, optional arguments, generics, callbacks, and several other common modern Fortran constructs.
Compared with the f2py route, PRIK focuses on letting you design a more Pythonic API (renames, methods, return-value projection, hidden arguments, etc.) while still generating a native extension.
I have also been testing PRIK against real libraries rather than only small wrapper tests. The current validation suite includes:
- BLAS — 155 routines
- LAPACK — 127 double-precision routines
- FFTPACK — 31 procedures
- MINPACK — 22 procedures, including callbacks
- BSPLINE-FORTRAN — including modern Fortran classes
There is also C support, currently with a smaller supported surface, with real-library tests against libm and TA-Lib.
PRIK is still alpha, so the API may change before 1.0. Fortran support is currently considerably more mature than C support.
Documentation:
GitHub: