This is an archival dump of old wiki content --- see for current material.
Please see

This page provides examples on how to use the F2py fortran wrapping program.

It is possible to start with simple routines or even to wrap full Fortran modules. F2py is used in SciPy itself and you can find some examples in the source code of SciPy.

Short examples

Wrapping a function from lapack

Taken from a message on 2006-06-22 to scipy-user by ArndBaecker

Thanks to f2py, wrapping Fortran code is (with a bit of effort) trivial in many cases. For complicated functions requiring many arguments the wrapper can become longish. Fortunately, many things can be learnt from looking at scipy/Lib/linalg/generic_flapack.pyf In particular, the documentation at is excellent. I also found the f2py notes by FernandoPerez very helpful,

Let me try to give some general remarks on how to start (the real authority on all this is of course Pearu, so please correct me if I got things wrong here):

  from scipy.lib import lapack
  lapack.clapack.<TAB>         (assuming Ipython)

  import wrap_lap

Concrete (and very simple) example (non-lapack):

Wrapping Hermite polynomials

Download code (found after hours of googling ;-) , from

and extract hermite.f

Generate wrapper framework:

  # only run the following line _once_
  # (and never again, otherwise the hand-modified hermite.pyf
  #  goes down the drains)
  f2py -m hermite -h hermite.pyf hermite.f

Then modify hermite.pyf

Create the module:

  f2py -c hermite.pyf  hermite.f

  # add this if you want:

Simple test:

  import hermite
  import scipy

A more complicated example about how to wrap routines for band matrices can be found at under "Python and Co - some recent developments".

Wrapping a simple C code

f2py is also capable of handling C code. An example is found on the wiki: Cookbook/f2py_and_NumPy.

Step by step wrapping of a simple numerical code: Interactive System for Ice sheet Simulation

CategoryCookbook CategoryCookbook

SciPy: Cookbook/F2Py (last edited 2015-10-24 17:48:24 by anonymous)