f2py and the tools not in this image
Every course in this site meets the same problem at its Python border: the tool that automates the binding is not installed, and the honest answer is to say so and show the route that works without it. For Fortran that tool is f2py, and it needs numpy. This page is about what f2py would do, why it is not run here, and what was run instead.
What f2py does
f2py reads Fortran source, works out the argument lists, and generates a C wrapper plus a Python module that calls it through numpy's array interface. One command produces an importable extension: python3 -m numpy.f2py -c -m hypmod hypmod.f90 would build a module called hypmod in this directory, and a Python program would then call hypmod.hyp(3.0, 4.0).
It knows about arrays, shapes, intent, optional arguments and derived types, and it writes the .pyf signature file it inferred, which a project can edit and keep. That is the reason a scientific Python package with a Fortran core uses it: the wrapper is generated from the same source that the Fortran tests build.
The source it would wrap
A plain module with a plain function, no bind(c) and no interface block, is what f2py is given; it generates the boundary itself. This file compiles here, which is the only claim the page makes about it:
module hypmod
use, intrinsic :: iso_fortran_env, only: real64
implicit none
contains
pure function hyp(a, b) result(y)
real(real64), intent(in) :: a, b
real(real64) :: y
y = sqrt(a * a + b * b)
end function hyp
end module hypmod
gfortran -Wall -Wextra -std=f2018 -c hypmod.f90
ls hypmod.mod
hypmod.mod
No warning and one file: the module compiles and writes the hypmod.mod that a wrapper would have to link against. The next command is the one that is not run on this page, and it is not run because the tool it needs is not in the image.
Not run here
f2py is part of numpy, and numpy is not installed in this container. Two commands show it, and both are real output from this image:
python3 -c "import numpy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
python3 -m numpy.f2py --version
/usr/bin/python3: Error while finding module specification for 'numpy.f2py' (ModuleNotFoundError: No module named 'numpy')
So there is no f2py output on this page, no generated wrapper and no module called hypmod that Python could import. A page that showed such a run would be inventing it, and the rest of this course is built on the rule that no output is written unless it was produced.
The same is true of the other tools this course names in words only: fpm (lesson 112), pFUnit, test-drive and fortran-stdlib (the testing group after the course), and OpenCoarrays (lesson 101). Each page says which part of its subject is a run and which is a description.
The install route
On a machine with network access the route is two commands and a compiler: pip install numpy in a virtual environment, then python3 -m numpy.f2py -c -m hypmod hypmod.f90 from the directory that holds the source. The build needs the same gfortran and the same Python development headers that any extension module needs, and it produces a file named hypmod.cpython-314-x86_64-linux-musl.so in this Python's terms.
This image has pip 26.1.2 for Python 3.14.10, so the first command would work if there were a package index to reach; there is not, and the course does not install anything into the image. A reader who wants the f2py route should run it on their own machine and compare the numbers with lesson 107: the same function, two bindings, and a result that must agree.
The route that was run
Lesson 107 built the library and called it, and its numbers are real. The comparison is worth keeping in mind: ctypes needs a shared object and a description of each function in Python, and nothing else; f2py needs numpy and generates the description from the source. For two functions the ctypes route is shorter; for a module of forty procedures with arrays and optional arguments the generated wrapper is the only sane option.
Both routes end at the same place, a C symbol with a calling convention, which is why the two lessons before this one are the groundwork for either. A page that teaches ctypes and calls it equivalent to f2py would be wrong about the work and right about the mechanism.
Which one to choose
A handful of scalar functions: ctypes, no dependency, twenty lines. A library with arrays, optional arguments and derived types: f2py if numpy is acceptable as a dependency, which in a scientific Python program it usually is. A program that must not depend on numpy: a hand-written C wrapper on top of bind(c), which is lessons 105 and 106 with a module in between.
What none of them removes is the test. Whatever the binding, the first call has to be compared with a value computed on one side alone, because a wrong binding does not fail: it returns something, and the something is a number.
Exercises
- Write the .pyf signature file that f2py would generate for hypmod, with the types and intents of both arguments. What does it say about the result?
- Compile hypmod.f90 into a shared library and call it with ctypes, using lesson 107 as the model. Which description does hyp need?
- Explain in one sentence why a page cannot show the output of a tool that is not installed, using this page as the example.
- Name two other tools this course mentions but does not run, and the lesson that says so.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does f2py generate?
Answer
A C wrapper and a Python extension module for a Fortran source
It infers the interface and builds the module.
-
Why is f2py not run on this page?
Answer
numpy is not installed in the image
The page shows the real ModuleNotFoundError.
-
What does
python3 -c "import numpy"print here?Answer
ModuleNotFoundError: No module named numpy
It is the real output on this page.
-
Which route was run instead?
Answer
ctypes on a shared library built with -shared -fPIC
Lesson 107 has the numbers.
-
What does a ctypes binding need that f2py generates?
Answer
The description of each function: argtypes and restype
The description is the work f2py automates.
-
When is f2py the better choice?
Answer
For a large module with arrays and optional arguments
The generated wrapper scales, the hand-written one does not.
-
What does a
.pyffile hold?Answer
The signature f2py inferred, which can be edited and kept
It is the interface description in a file.
-
Which other tool does this course name in words only, with a lesson that says so?
Answer
fpm, in lesson 112
OpenCoarrays and the testing tools are the others.
-
Why must the first call of any binding be compared with a known value?
Answer
A wrong binding returns a plausible number instead of failing
The numbers of lesson 107 are the check.
-
What does the page say about inventing tool output?
Answer
It never does it; a page shows what was run and says what was not
Every output in the course is from a real run.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev