Fortran & Python

26.1 f2py (part of NumPy)

f2py handles intent(in/out/inout/hide) directives, module variables, and callbacks.

subroutine fib(a, n)
  integer, intent(in) :: n
  real(8), intent(out) :: a(n)
  integer :: i
  a(1) = 0; if (n > 1) a(2) = 1
  do i = 3, n
    a(i) = a(i-1) + a(i-2)
  end do
end subroutine fib
python -m numpy.f2py -c fib.f90 -m fibmod      # uses meson as the build backend (NumPy >= 1.26)
import fibmod
print(fibmod.fib(10))      # array([ 0.,  1.,  1.,  2.,  3.,  5.,  8., 13., 21., 34.])

26.2 Other approaches

ctypes / cffi with bind(c) procedures compiled into a shared library.

Cython wrapping bind(c) interfaces.

gfort2py, fmodpy, f90wrap (supports derived types).

LFortran can emit Python-callable modules and run Fortran in Jupyter.

Calling Fortran from Julia (ccall), R (.Fortran() / .Call), MATLAB (MEX).