Collected material

tags: planning

Python API generators

f2py

Python wrapper generator that is part of NumPy

Main limitations:

  • Doesn't support derived types

f90wrap

Fortran to Python interface generator that extends f2yp with derived type support.

fmodpy

"An easy-to-use Fortran wrapper for Python." Still in development.

gfort2py

Library to allow calling fortran code from python.

  • Only works with gfortran

F2x

"A versatile, template-based Fortran wrapper written in Python."

  • Lacks documentation

Forthon

"Forthon generates links between Fortran95 and Python."

  • Lacks documentation

SWIG-Fortran

  • Fairly well supported but is still an unfriendly fork with no path to being merged upstream

Various packages that don't seem to be maintained

fwrap

C interfaces from Python

ctypes

Foreign function library in the Python standard library.

cffi

Python wrapper generator for C

Cython

Compiler for Python C-extensions. Can load shared libaries.

Making your C library callable from Python by wrapping it with Cython

numpy.ctypeslib

Module to facilitate working with NumPy arrays in C interfaces

numpy.ctypeslib.as_ctypes
Converting array to C array

numpy.ctypeslib.as_array
Convert C array to NumPy array

numpy.ctypeslib.load_library
Alternative to ctypes.cdll that takes care of platform-specific things

numpy.ndarray.ctypes

An object to simplify the interaction of NumPy arrays with the ctypes module

x.ctypes.data
Attribute to access data as C array

SWIG

Python wrapper generator for C

Tools for patching Python wheels with dynamic libraries

  • Building Python packages with shared libraries means that the library needs to be present at runtime for the end user.
  • Tools are available to patch the binary wheels to include these shared libraries.
  • Support for Windows is currently lacking.

delocate

Well-established tool for MacOs

auditwheel

Well-established tool for Linux

delvewheel

Preliminary tool for Windows

Available guides

Calling Fortran from Python
Fortran90.org: Interfacing with Python
Fortran Wiki: Generating C Interfaces
Fortran Wiki: Python
Foreign Fortran
Python-Fortran bindings examples
Interfacing Fortran code
Using fortran from python
High-Performance Python โ€“ Compiled Code and Fortran Interface
Combining Python with Fortran, C and C++
Writing fast Fortran routines for Python
Interfacing With Other Programming Languages Using Cython

Slides

Interfacing Python with Fortran
Slides from INF3331 lectures โ€“ combining Python with Fortran/C/C++

Running Python from Fortran

forcallpy
forpy
call_py_fort