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 needs something the image lacks, and the honest answer is to say exactly which part runs and which does not. For Fortran that tool is f2py, which ships with numpy: numpy is installed here, so the wrapper is generated, while the build step needs meson, which is not. This page shows both halves of that and the ctypes route that 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.
What runs here and what does not
numpy is installed in this image, so f2py itself runs; the version is worth printing first, because the build backend changed between numpy 1 and numpy 2:
python3 -c "import numpy; print(numpy.__version__)"
2.4.6
Without -c f2py reads the Fortran and writes the C wrapper, which is the half that needs no build tool:
python3 -m numpy.f2py -m hypmod hypmod.f90 2>&1 | tail -3
ls hypmodmodule.c
y = hyp(a,b)
Wrote C/API module "hypmod" to file "./hypmodmodule.c"
Fortran 90 wrappers are saved to "./hypmod-f2pywrappers2.f90"
hypmodmodule.c
With -c it also has to compile and link the extension, and in numpy 2.x that step goes through meson. meson is not installed here, and there is no index from which the page could fetch it:
python3 -m numpy.f2py -c -m hypmod hypmod.f90 2>&1 | grep -i meson
File "/usr/lib/python3.14/site-packages/numpy/f2py/_backends/_meson.py", line 197, in compile
self.run_meson(self.build_dir)
File "/usr/lib/python3.14/site-packages/numpy/f2py/_backends/_meson.py", line 189, in run_meson
File "/usr/lib/python3.14/site-packages/numpy/f2py/_backends/_meson.py", line 185, in _run_subprocess_command
FileNotFoundError: [Errno 2] No such file or directory: 'meson'
Cannot use distutils backend with Python>=3.12, using meson backend instead.
Using meson backend
See https://numpy.org/doc/stable/f2py/buildtools/meson.html
So the wrapper is written and no module is built: there is no hypmod that Python could import on this page, and a page that showed one would be inventing it. The rest of this course is built on the same rule: an output is written only when it was produced.
The same is true of the other tools this course names in words only: fpm (lesson 12.3), pFUnit, test-drive and fortran-stdlib (the testing group after the course), and OpenCoarrays (lesson 10.4). 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 meson ninja in the environment that already has numpy, 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 and numpy 2.4.6, 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 11.4: the same function, two bindings, and a result that must agree.
The route that was run
Lesson 11.4 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 11.2 and 11.3 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 11.4 as the model. Which description does hyp need?
- Explain in one sentence why a page cannot show the output of a step whose tool is not installed, using the meson step of 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.
-
What stops the f2py build step on this page?
Answer
The meson build backend is not installed
numpy 2.4.6 is here; the -c step dies with FileNotFoundError: meson.
-
What does
python3 -c "import numpy; print(numpy.__version__)"print here?Answer
2.4.6
That is the numpy the image carries, as the page shows.
-
Which route was run instead?
Answer
ctypes on a shared library built with -shared -fPIC
Lesson 11.4 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 12.3
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 11.4 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