The Using via meson page covers building F2PY extensions using raw meson
commands. This page shows how to package those extensions into installable
Python distributions (sdists and wheels) using meson-python as the PEP 517 build backend.
This is the recommended approach for distributing F2PY-wrapped Fortran code as a
Python package on PyPI or for local pipinstall workflows.
subroutine fib(a,n)use iso_c_bindinginteger(c_int),intent(in)::ninteger(c_int),intent(out)::a(n)integer::ido i=1,nif(i==1)thena(i)=0else if(i==2)thena(i)=1elsea(i)=a(i-1)+a(i-2)end if end doend subroutine fib
pyproject.toml
[build-system]# numpy>=2.0 is required for dependency('numpy') support in meson.buildrequires=["meson-python>=0.15.0","numpy>=2.0"]build-backend="mesonpy"[project]name="fib_wrapper"version="0.1.0"requires-python=">=3.10"
A minimal __init__.py re-exports the wrapped function:
from.fibimportfib
Building and installing
Editable install (development)
pip install --no-build-isolation --editable .
--no-build-isolation reuses the current environment, which is useful when
iterating. This requires meson-python, meson, ninja, and numpy
to already be installed.
Building a wheel
# If you don't yet have `pypa/build` installed: `pip install build`
python-mbuild--wheel
The resulting .whl file in dist/ can be uploaded to PyPI, or installed
elsewhere with pipinstalldist/fib_wrapper-0.1.0-*.whl.
meson-python delegates compiler selection to meson. By default,
meson will choose the first Fortran compiler it finds on the PATH.
If you want more control over Fortran compiler selection, set the FC
environment variable before building:
build-backend="mesonpy" tells build frontends to use meson-python.
requires lists build-time dependencies. numpy>=2.0 is required so
that f2py, the NumPy headers, and dependency('numpy') support in Meson
are available during compilation.
import
(
'python'
)
.
find_installation
(
pure
:
false
)
# NumPy >=2.0 provides include dirs via dependency()
# f2py include dir (for fortranobject.h) is not in dependency('numpy'),
# so add it separately
f2py_dep
=
declare_dependency
(
include_directories
:
incdir_f2py
,
)
# Generate the f2py wrappers
fib_source
=
custom_target
(
'fibmodule.c'
,
input
:
[
'fib.f90'
],
output
:
[
'fibmodule.c'
,
'fib-f2pywrappers.f'
],
command
:
[
py
,
'-m'
,
'numpy.f2py'
,
'@INPUT@'
,
'-m'
,
'fib'
,
'--lower'
]
)
py
.
extension_module
(
'fib'
,
[
'fib.f90'
,
fib_source
,
incdir_f2py
/
'fortranobject.c'
],
dependencies
:
[
np_dep
,
f2py_dep
],
subdir
:
'fib_wrapper'
,
install
:
true
,
)
# Install the Python package files
py
.
install_sources
(
'fib_wrapper/__init__.py'
,
subdir
:
'fib_wrapper'
,
)
The meson.build file does four things:
Uses dependency('numpy') to locate NumPy headers, and a
declare_dependency to add the F2PY include directory (for
fortranobject.h).
Runs f2py via custom_target to generate the C wrapper sources.
Compiles the generated C code together with the Fortran source into a Python
extension module using py.extension_module.
Installs __init__.py into the package directory so the result is a proper
Python package.
The subdir:'fib_wrapper' argument on the extension module is required so
that the compiled fib shared library is installed inside the fib_wrapper/
package directory, next to __init__.py. Without it the extension would
be installed at the top level and importfib_wrapper would not find the
fib extension. The resulting installed layout is: