b

Setting up and using F2PY

For a research project we wished to wrap an Old-Ironsides-eqsue FORTRAN90 program into a slick and modern python module that could be imported and used in conjuction with pytables. This proved to be a rather monsterous task as the F2PY parser has a hard time figuring out quite what to do with the code unless it's either very obvious or specifically told by directives sprinkled throughout the fortran source in the form of specialized comments.

In order to even get started, we have to install a handful of packages. Assuming python is already happily installed, this boils down to the following set of rather suspect packages, installed as such:
sudo apt-get install python-numpy python-dev gfortran fort77 python-numpy-ext
Python-dev is most certianly necessary, and why is it not included as a requirement for python-numpy, I'll never know. Without it F2PY spouts rather frightening errors like the following:

[ Show crazy output / Woah! Hide that noise! ]

/usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:181: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:182: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘npy_uintp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:284: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:285: error: expected ‘)’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:287: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:287: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:288: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:298: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:298: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:300: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:300: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:301: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:303: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:308: error: expected ‘)’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:313: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:315: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:316: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:316: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:318: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:322: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:324: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:325: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:326: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:327: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:327: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:328: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:328: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:332: error: expected specifier-qualifier-list before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:345: error: expected specifier-qualifier-list before ‘PyArray_GetItemFunc’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:446: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:478: error: expected specifier-qualifier-list before ‘PyObject’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:488: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:513: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:521: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:528: error: expected declaration specifiers or ‘...’ before ‘PyObject’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:670: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:817: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:871: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:1084: error: expected specifier-qualifier-list before ‘npy_intp’ In file included from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:1099, from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/arrayobject.h:14, from /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:13, from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h: In function ‘_import_array’: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: ‘PyObject’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: (Each undeclared identifier is reported only once /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: for each function it appears in.) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: ‘numpy’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: warning: implicit declaration of function ‘PyImport_ImportModule’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:972: error: ‘c_api’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:972: error: ‘NULL’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:974: warning: implicit declaration of function ‘PyObject_GetAttrString’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:975: warning: implicit declaration of function ‘Py_DECREF’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:976: warning: implicit declaration of function ‘PyCObject_Check’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:977: warning: implicit declaration of function ‘PyCObject_AsVoidPtr’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:984: warning: implicit declaration of function ‘PyErr_Format’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:984: error: ‘PyExc_RuntimeError’ undeclared (first use in this function) In file included from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h: At top level: /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:61: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:63: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:73: error: expected specifier-qualifier-list before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:85: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:94: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyFortran_Type’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:95: error: expected ‘)’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:96: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:111: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:114: error: expected declaration specifiers or ‘...’ before ‘PyObject’ /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:100: error: ‘NULL’ undeclared here (not in a function) /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:103: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘f2py_module_methods’ /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:108: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘inittiny_program’ /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:16:20: error: Python.h: No such file or directory In file included from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:61, from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/arrayobject.h:14, from /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:13, from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/npy_common.h:71:2: error: #error Must use Python with unicode enabled. In file included from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/arrayobject.h:14, from /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:13, from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:181: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:182: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘npy_uintp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:284: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:285: error: expected ‘)’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:287: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:287: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:288: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:298: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:298: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:300: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:300: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:301: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:303: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:308: error: expected ‘)’ before ‘*’ token /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:313: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:315: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:316: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:316: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:318: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:322: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:324: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:325: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:326: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:327: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:327: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:328: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:328: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:332: error: expected specifier-qualifier-list before ‘npy_intp’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:345: error: expected specifier-qualifier-list before ‘PyArray_GetItemFunc’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:446: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:478: error: expected specifier-qualifier-list before ‘PyObject’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:488: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:513: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:521: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:528: error: expected declaration specifiers or ‘...’ before ‘PyObject’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:670: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:817: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:871: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:1084: error: expected specifier-qualifier-list before ‘npy_intp’ In file included from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:1099, from /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/arrayobject.h:14, from /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:13, from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h: In function ‘_import_array’: /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: ‘PyObject’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: (Each undeclared identifier is reported only once /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: for each function it appears in.) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: error: ‘numpy’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:971: warning: implicit declaration of function ‘PyImport_ImportModule’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:972: error: ‘c_api’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:972: error: ‘NULL’ undeclared (first use in this function) /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:974: warning: implicit declaration of function ‘PyObject_GetAttrString’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:975: warning: implicit declaration of function ‘Py_DECREF’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:976: warning: implicit declaration of function ‘PyCObject_Check’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:977: warning: implicit declaration of function ‘PyCObject_AsVoidPtr’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:984: warning: implicit declaration of function ‘PyErr_Format’ /usr/lib/python2.6/dist-packages/numpy/core/include/numpy/__multiarray_api.h:984: error: ‘PyExc_RuntimeError’ undeclared (first use in this function) In file included from /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:17: /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h: At top level: /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:61: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:63: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:73: error: expected specifier-qualifier-list before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:85: error: expected specifier-qualifier-list before ‘PyObject_HEAD’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:94: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyFortran_Type’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:95: error: expected ‘)’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:96: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:97: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:111: error: expected declaration specifiers or ‘...’ before ‘npy_intp’ /tmp/tmpWxV0ti/src.linux-i686-2.6/fortranobject.h:114: error: expected declaration specifiers or ‘...’ before ‘PyObject’ /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:100: error: ‘NULL’ undeclared here (not in a function) /tmp/tmpWxV0ti/src.linux-i686-2.6/tiny_programmodule.c:103: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘f2py_module_method

Once those packages are installed, test that gfortran and F2PY is functioning properly. In order for gfortran to not throw a fit upon compilation the needs to have a main loop, that is, if simply ran it needs to DO something rather than just define a bunch of functions (though the bunch of functions is all we really want for F2PY purposes). As such, this works a fine test program:
!shortest_example.f90 MODULE Example CONTAINS FUNCTION Increment(x) INTEGER, INTENT(IN) :: x Increment = x + 1 END FUNCTION Increment END MODULE Example PROGRAM Test PRINT *, 'Why hello there.' END PROGRAM Test
Notice that without the pointless test program block at the end, attempting to compile this with gfortran yields the following mess:

[ Show crazy output / Woah! Hide that noise! ]

alejandero@jalopy:~$gfortran shortest_example.f90 -o output /usr/lib/gcc/i486-linux-gnu/4.4.3/libgfortranbegin.a(fmain.o): In function `main': (.text+0x27): undefined reference to `MAIN__' collect2: ld returned 1 exit status

The previous short program should be able to compile to a python module by running the following command, and the functions would be addressed as in the following script.
alejandero@jalopy:~$f2py -c -m mymod shortest_example.f90
#Module import and test program - modtest.py from mymod import * print example.increment(9)
The goal is to do something more interesting with FORTRAN90, namely really fast array operations, the one thing FORTRAN is really good at. To do this we need to be fairly careful about how to give F2PY the hint that we're dealing with several inputs of different types, as well as keeping FORTRAN informed. The key here is that both need know the size and shape of the arrays moving back and forth between them, as they handle memory space differently and essentially just pass memory addresses to eachother.
!second_example.f90 MODULE ArrayStuff IMPLICIT NONE CONTAINS FUNCTION Increment(len_x, x) IMPLICIT NONE INTEGER, INTENT(IN) :: len_x REAL, DIMENSION(len_x), INTENT(IN) :: x REAL, dimension(len_x) :: Increment !f2py depend(len_x) x, Increment Increment = x + 1 END FUNCTION Increment END MODULE ArrayStuff
This program takes an array of a given length and adds 1 to each of the elements. From the python end it can be fed an integer for length, and a list, tuple, or array (from the numpy module) for the fortan array. Without the commented line containing the f2py directive that tells it that the dimensions of the output depend on len_x, it will pass through f2py without complaints, but when we attempt to use it, we get the following:

[ Show crazy output / Woah! Hide that noise! ]

In [1]: from mymod import * In [2]: arraystuff.increment(3,[1,2,3]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /home/alejandero/ in () ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1,)