f2py
Create Python extensions from Fortran code
SYNOPSIS
f2py [options] fortran_source_file...
f2py -h signature_file [options] fortran_source_file...
f2py -c [build_options] fortran_source_file... [-m module_name]
PARAMETERS
-c
Compile the Fortran source files into a Python extension module. This is the most common usage for building binary modules.
-m module_name
Specify the name of the resulting Python module (e.g., -m mymodule creates mymodule.so or mymodule.pyd).
-h signature_file
Generate a signature file (a .pyf file) that describes the Fortran interface to Python, instead of compiling. This file can be manually edited.
--fcompiler=compiler
Specify the Fortran compiler to use (e.g., gfortran, intel, or path to executable).
--opt='options'
Pass additional optimization flags or compiler-specific options to the Fortran compiler during compilation.
--verbose
Print detailed information and debug messages about the compilation process, helping troubleshoot issues.
DESCRIPTION
f2py (Fortran to Python Interface Generator) is a powerful command-line tool that facilitates the integration of Fortran code into Python programs. It parses Fortran source files (supporting Fortran 77, 90, and 95), automatically generating a Python extension module that allows Python code to call Fortran subroutines and functions directly. f2py handles the intricate details of argument passing, automatic data type conversion (e.g., Fortran arrays to NumPy arrays), and memory management, significantly simplifying the process of creating Python bindings for high-performance Fortran numerical routines.
It is an integral part of the NumPy package, commonly used to leverage existing Fortran libraries or write performance-critical sections of code in Fortran while maintaining a Pythonic interface, accelerating scientific and engineering computations.
CAVEATS
f2py relies on a functional Fortran compiler (such as gfortran or Intel Fortran) being installed and discoverable on the system path.
While powerful, integrating complex Fortran features like advanced pointers, highly customized derived types, or intricate global common blocks might require manual adjustments to the generated signature files.
Debugging can be challenging as it involves interaction between both Python and Fortran codebases. Additionally, the generated modules are heavily reliant on NumPy for efficient array manipulation and expect NumPy to be installed.
SIGNATURE FILE (<I>.PYF</I>)
When invoked with the -h option, f2py generates a signature file (with a .pyf extension). This file describes the Fortran interface to Python, detailing subroutines, functions, arguments, and their types. It can be manually edited to fine-tune the interface, add callbacks, or handle specific Fortran constructs, providing granular control over the generated Python module before actual compilation.
HISTORY
f2py was originally developed by Pearu Peterson and later became an integral part of the SciPy and NumPy ecosystem. Its development aimed to provide a robust and user-friendly mechanism for Python users to leverage existing high-performance Fortran libraries, which were prevalent in scientific and engineering computations. It has continuously evolved, supporting newer Fortran standards and adapting to changes in the Python scientific stack, making it a foundational tool for bridging these two powerful programming paradigms and facilitating high-performance computing in Python.