F2PY user guide and reference manual#

The purpose of theF2PYFortran to Python interface generator– utilityis to provide a connection between Python and Fortran. F2PY distributed as partofNumPy (numpy.f2py) and once installed is also available as a standalonecommand line tool. Originally created by Pearu Peterson, and older changelogsare in thehistorical reference.

F2PY facilitates creating/building nativePython C/API extension modules thatmake it possible

  • to call Fortran 77/90/95 external subroutines and Fortran 90/95module subroutines as well as C functions;

  • to access Fortran 77COMMON blocks and Fortran 90/95 module data,including allocatable arrays

from Python.

Note

Fortran 77 is essentially feature complete, and an increasing amount ofModern Fortran is supported within F2PY. Mostiso_c_binding interfacescan be compiled to native extension modules automatically withf2py.Bug reports welcome!

F2PY can be used either as a command line toolf2py or as a Pythonmodulenumpy.f2py. While we try to provide the command line tool as partof the numpy setup, some platforms like Windows make it difficult toreliably put the executables on thePATH. If thef2py command is notavailable in your system, you may have to run it as a module:

python-mnumpy.f2py

Using thepython-m invocation is also good practice if you have multiplePython installs with NumPy in your system (outside of virtual environments) andyou want to ensure you pick up a particular version of Python/F2PY.

If you runf2py with no arguments, and the linenumpyVersion at theend matches the NumPy version printed frompython-mnumpy.f2py, then youcan use the shorter version. If not, or if you cannot runf2py, you shouldreplace all calls tof2py mentioned in this guide with the longer version.

For Meson build examples, seeUsing F2PY.