[SciPy-user] problem with getting F2Py to work

Pearu Peterson pearu at cens.ioc.ee
Tue Sep 10 17:02:39 EDT 2002


On Tue, 10 Sep 2002, Perrin Meyer wrote:

> Hi.
> 
> I recently attended the Scientific Python conference at CalTech, and
> there I was told about the F2Py package for wrapping numerical
> C/Fortran code for use in Python.
> 
> I'm having a little trouble getting it working.
> 
> I'm trying to wrap the simple C function psmtest.c:
> 
> extern void psmtestfunc(double * A,int *m) {
>   int i;
>   for (i=0 ; i < *m ; i++) {
>     A[i] = A[i] * 0.4534534e-13;
>   }
> }
> 
> I use the Fortran 90 Signature file psmtest.pyf:
> 
> python module perrin
>   interface
>      subroutine psmtestfunc(A,m)
>        integer m
>        real*8 A(m)
>      end subroutine psmtestfunc
>   end interface
> end module perrin

You are almost there. Use the following signature file:

python module perrin
  interface
    subroutine psmtestfunc(a,m)
      intent(c) psmtestfunc 
      integer intent(hide),depend(a) :: m = len(a)
      real*8 intent(in,out)          :: a(m)
    end subroutine psmtestfunc
  end interface
end module perrin

Few comments:
`intent(c) psmtestfunc' --- tells to f2py that psmtestfunc is a C function
`intent(hide)'  --- tells to f2py not to include m to the argument list...
`depend(a) :: m = len(a)' --- ... because the value of m is determined by
                the argument a.
`intent(in,out) :: a' --- a is input and after running the C function it
                should be returned.

>From this pyf file f2py generates a function that is equivalent to
the following Python function:

def psmtestfunc(a):
    a = a * 0.4534534e-13
    return a

> ________________________________________________________________________
> 
> which creates the perrinmodule.c file (attached).
> 
> I compile with:
> 
> gcc -shared -o perrinmodule.so -I 
> /home/perrin/PYTHON_INSTALL/include/python2.2/ -I 
> /home/perrin/PYTHON_INSTALL/lib/python2.2/site-packages/f2py2e/src/  \
> -L /home/perrin/PYTHON_INSTALL/lib/python2.2/site-packages/Numeric  
> psmtest.c perrinmodule.c
> 
> to create the file perrinmodule.so

Use f2py to build the extension module as follows:

  f2py -c psmtest.pyf psmtest.c

This should create perrin.so into the current directory. Now run python
and try the following session:

>>> import perrin
>>> perrin.psmtestfunc([1,2,3,4])
array([  4.53453400e-14,   9.06906800e-14, 1.36036020e-13,  1.81381360e-13])
>>> print perrin.psmtestfunc.__doc__

> When I try to import the module into Python, I get the following error 
> message:
> 
> [perrin at wave PSM_TEST]$ psmpython
> Python 2.2.1 (#1, Aug 23 2002, 12:31:50)
> [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> from Numeric import *
>  >>> import Numeric
>  >>> from perrin import *
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ImportError: ./perrinmodule.so: undefined symbol: array_from_pyobj
>  >>>
> 
> I guess I'm probably linking wrong? I don't know much about creating 
> shared object libraries.

f2py provides array_from_pyobj that is in f2py2e/src/fortranobject.c.
However, if you use `f2py -c' to build extension modules then f2py does
the "right" thing automatically.

Regards,
	Pearu




More information about the SciPy-User mailing list