Bridging Fortran and Python: A Practical Mini-Course and Resources

Hi everyone,

I delivered a 4-hour mini-course last year on integrating Fortran with Python, and since this is a topic that’s frequently discussed here on Discourse, I thought it would be worth sharing the materials. There are several threads on this subject here, and I’ve gathered some useful insights from those discussions to inform the material I created. I believe this content could be helpful to anyone looking for a practical approach to Fortran-Python integration.

The course covers Cython, ctypes, and CFFI for interfacing Fortran and Python, and I’ve made the materials available on my GitHub (GitHub - ofmla/interfacing_fortran_from_python: Training course -- Interfacing Fortran and Python: Using cffi, ctypes and cython) for anyone interested in exploring them further.

I welcome comments, corrections, and additions from the community!

12 Likes

Really nice!

I usually build a shared library with my Fortran code with a C interface and then plug it into Python through a C extension written by hand. It is not the fastest solution because you need to write C code with the CPython API. Nonetheless, the type casting and type conversion is done at the C level which limits the overhead.

My setup.py file pulls all the GFortran libraries needed (for windows and MacOS) and everything is packed in the wheel.

You can see an example on my GitHub repo e.g. GitHub - MilanSkocic/ciaaw: Isotopic compositions and atomic weights for Modern Fortran.

1 Like