For an intro to f2py see here.

It’s possible to combine multiple Fortran modules into a single shared library to be used by Python using f2py. It’s actually pretty easy, but I’m writing a note about it because I’ll likely forget how to do it in the future.

Let’s suppose we have two modules, one.f90 and two.f90. one uses two, and we only explicitly want to use the functions in one from Python.

For example, one.f90 might contain:

module one
use two


function onefunc(x) result(y)
    real(8) :: x,y
    y = twofunc(x)
    y = twofunc(y)
end function

end module

… while in two.f90:

module two
implicit none


function twofunc(x) result(y)
    real(8) :: x,y
    y = x*x
end function

end module

To compile, execute:

gfortran -c two.f90
f2py -c two.f90 one.f90 -m one

Now, check that it works in python

>>> from one import one
>>> one.onefunc(3)
>>> # 81.0