LinuxCommandLibrary

f2py

Create Python extensions from Fortran code

SYNOPSIS

f2py [options] fortran_source_files [signature_files] [-m module_name]

PARAMETERS

-h, --help
    Display help message and exit

-v, --verbose
    Increase verbosity level (use multiple times for more detail)

-c
    Compile to object file and link into shared library (.so)

-m name
    Set module name (default from first file)

--fcompiler=compiler
    Specify Fortran compiler (e.g., gnu95, intel)

-l lib
    Link against library lib (e.g., -lm)

-Dmacro[=value]
    Define preprocessor macro

-Idir
    Add include directory for fixed-format Fortran

--opt="opts"
    Optimization flags (e.g., -O3)

-Ldir
    Library search directory

--no-wrap-functions
    Disable function wrapper generation

--quiet
    Suppress status messages

DESCRIPTION

f2py is a command-line tool from the NumPy SciPy stack that generates Python extension modules from Fortran 77/90/95 source code. It scans Fortran files to extract function signatures, creates C wrapper code for seamless Python-Fortran interoperability, compiles the code into a shared library (.so on Linux), and enables calling Fortran routines from Python with NumPy array support.

Ideal for scientific computing, f2py bridges legacy Fortran libraries (e.g., BLAS, LAPACK) with modern Python workflows. Users provide Fortran .f or .f90 files and optional signature files (.pyf) for precise control over interfaces. On Linux, it auto-detects Fortran compilers like gfortran, handles linking, and supports modules, callbacks, and multidimensional arrays. The resulting module imports like any Python package, boosting performance for numerical tasks without rewriting code.

CAVEATS

Requires Fortran compiler (e.g., gfortran) and NumPy. Limited support for advanced Fortran 2003+ features, COMMON blocks, or complex pointers. Fixed-format Fortran may need -fixed option. Debug with -DF2PY_REPORT_ATEXIT.

BASIC USAGE EXAMPLE

f2py -c -m mymod file.f
Builds mymod.so from file.f; import as import mymod.

SIGNATURE FILES

Use .pyf files for custom interfaces:
f77name - callstatement, intent(...), etc.

HISTORY

Developed by Pearu Peterson in 2000 as part of NumPy. Evolved with SciPy integration; major updates in NumPy 1.x for better F90 support and compiler detection. Maintained by NumPy team; version tied to NumPy (e.g., f2py3.10 with Python 3.10).

SEE ALSO

gfortran(1), gcc(1), python3(1), ld(1)

Copied to clipboard