From Peter Jan Randewijk
Jump to: navigation, search


F2PY: Fortran to Python interface generator


The purpose of the F2PY --Fortran to Python interface generator-- project is to provide connection between Python and Fortran languages. F2PY is a Python extension tool for creating Python C/API modules from (handwritten or F2PY generated) signature files (or directly from Fortran sources). The generated extension modules facilitate:

  • Calling Fortran 77/90/95, Fortran 90/95 module, and C functions from Python.
  • Accessing Fortran 77 COMMON blocks and Fortran 90/95 module data (including allocatable arrays) from Python.
  • Calling Python functions from Fortran or C (call-backs).
  • Automatically handling the difference in the data storage order of multi-dimensional Fortran and Numerical Python (i.e. C) arrays.

In addition, F2PY can build the generated extension modules to shared libraries with one command. F2PY uses the scipy_distutils module from SciPy that supports number of major Fortran compilers.

F2PY generated extension modules depend on NumPy package that provides fast multi-dimensional array language facility to Python.

see http://cens.ioc.ee/projects/f2py2e

Installing the required F2PY components

F2PY used to be a seperate package for Python, but has subsequently been incorporated into NumPy.

In order for F2PY to wrap a piece of Fortran code, a Fortran and a C compiler is required. At the moment the GNU set of compilers seems to be the best for cross platform development. They include:

These compiler are usually standard on a Linux based platform.

On a Windows™ platform, MinGW provides a quick and easy way of getting g77 and gcc in one go while a g95 installer for Windows™ can be downloaded here.

Alternatively, on a Windows™ platform:

could also be used.

Getting help with F2PY

With NumPy installed, help for F2PY can be obtained either from within the Python interpreter in interactive mode:

>>> import numpy
>>> f2py.main()

1) To construct extension module sources:

      f2py [<options>] <fortran files> [[[only:]||[skip:]] \
                                        <fortran functions> ] \
                                       [: <fortran files> ...]

2) To compile fortran files and build extension modules:

      f2py -c [<options>, <build_flib options>, <extra options>] <fortran files>

3) To generate signature files:

      f2py -h <filename.pyf> ...< same options as in (1) >

Description: This program generates a Python C/API file (<modulename>module.c)
             that contains wrappers for given fortran functions so that they
             can be called from Python. With the -c option the corresponding
             extension modules are built.

or directly from the command prompt, by running the python script f2py:

Personal tools