Quantcast

f2py - how to use .pyf files?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

f2py - how to use .pyf files?

Fernando Perez
Howdy,

again f2py...  Since I can't seem to figure out how to pass the
--fcompiler option to f2py via setup.py/distutils, I decided to just
do things for now via a plain makefile.  But I'm struggling here too.
The problem is this: the call

f2py -c --fcompiler=gfortran -m text2 Text2.f90

works perfectly, and at some point in the output I see

customize Gnu95FCompiler
Found executable /usr/bin/gfortran

and the result is a nice text2.so module.  But I'd like to clean up a
bit the python interface to the fortran routines, so I did the usual

f2py -h text2.pyf Text2.f90

to create the .pyf, edited the pyf to adjust and 'pythonize' the
interface, and then when I try to build using this pyf, I get a crash
and the *same* gfortran option is now not recognized:

maqroll[felipe_fortran]> f2py -c --fcompiler=gfortran text2.pyf
Unknown vendor: "gfortran"
Traceback (most recent call last):
  File "/usr/bin/f2py", line 26, in <module>
    main()
  File "/home/fperez/usr/opt/lib/python2.5/site-packages/numpy/f2py/f2py2e.py",
line 560, in main
    run_compile()
  File "/home/fperez/usr/opt/lib/python2.5/site-packages/numpy/f2py/f2py2e.py",
line 536, in run_compile
    ext = Extension(**ext_args)
  File "/home/fperez/usr/opt/lib/python2.5/site-packages/numpy/distutils/extension.py",
line 45, in __init__
    export_symbols)
  File "/usr/lib/python2.5/distutils/extension.py", line 106, in __init__
    assert type(name) is StringType, "'name' must be a string"
AssertionError: 'name' must be a string


Note that it doesn't matter if I add Text2.f90 or not to the above
call, it still fails.

I could swear I'd done similar things in the past without any problems
(albeit with f77 sources), and the user guide

http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#three-ways-to-wrap-getting-started

gives instructions very much along the lines of what I'm doing.  Are
these changes since the integration into numpy, regressions, or
mistakes on how I'm calling it?

Thanks for  any help,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Robert Kern-2
On Fri, Jul 18, 2008 at 17:39, Fernando Perez <[hidden email]> wrote:

> Howdy,
>
> again f2py...  Since I can't seem to figure out how to pass the
> --fcompiler option to f2py via setup.py/distutils, I decided to just
> do things for now via a plain makefile.  But I'm struggling here too.
> The problem is this: the call
>
> f2py -c --fcompiler=gfortran -m text2 Text2.f90
>
> works perfectly, and at some point in the output I see
>
> customize Gnu95FCompiler
> Found executable /usr/bin/gfortran
>
> and the result is a nice text2.so module.  But I'd like to clean up a
> bit the python interface to the fortran routines, so I did the usual
>
> f2py -h text2.pyf Text2.f90

You still need "-m text2", I believe.

> to create the .pyf, edited the pyf to adjust and 'pythonize' the
> interface, and then when I try to build using this pyf, I get a crash
> and the *same* gfortran option is now not recognized:
>
> maqroll[felipe_fortran]> f2py -c --fcompiler=gfortran text2.pyf
> Unknown vendor: "gfortran"

It's --fcompiler=gnu95, not --fcompiler=gfortran

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Fernando Perez
On Fri, Jul 18, 2008 at 3:53 PM, Robert Kern <[hidden email]> wrote:

> You still need "-m text2", I believe.

Right, thanks.  But it still doesn't quite work.  Consider a makefile with

lib: seglib.so

seglib.so: Text2.f90
        f2py -c --fcompiler=gnu95 -m seglib Text2.f90

pyf: Text2.f90
        f2py -h seglib.pyf -m seglib Text2.f90 --overwrite-signature

lib2: Text2.f90
        f2py -c --fcompiler=gnu95 seglib.pyf

If I type

make lib

it works fine, but

make pyf
make lib2

bombs out with:

gfortran:f90: /tmp/tmpNgmzmT/src.linux-i686-2.5/seglib-f2pywrappers2.f90
/tmp/tmpNgmzmT/src.linux-i686-2.5/seglib-f2pywrappers2.f90:7.41:

      use seg_functions, only : g_tilde2d
                                        1
Fatal Error: Can't open module file 'seg_functions.mod' for reading at
(1): No such file or directory
/tmp/tmpNgmzmT/src.linux-i686-2.5/seglib-f2pywrappers2.f90:7.41:

      use seg_functions, only : g_tilde2d
                                        1
Fatal Error: Can't open module file 'seg_functions.mod' for reading at
(1): No such file or directory
error: Command "/usr/bin/gfortran -Wall -fno-second-underscore -fPIC
-O3 -funroll-loops -march=i686 -mmmx -msse2 -msse -msse3
-fomit-frame-pointer -malign-double
-I/tmp/tmpNgmzmT/src.linux-i686-2.5
-I/home/fperez/usr/opt/lib/python2.5/site-packages/numpy/core/include
-I/usr/include/python2.5 -c -c
/tmp/tmpNgmzmT/src.linux-i686-2.5/seglib-f2pywrappers2.f90 -o
/tmp/tmpNgmzmT/tmp/tmpNgmzmT/src.linux-i686-2.5/seglib-f2pywrappers2.o"
failed with exit status 1
make: *** [lib2] Error 1

Is it  obvious what I'm doing wrong?


>> to create the .pyf, edited the pyf to adjust and 'pythonize' the
>> interface, and then when I try to build using this pyf, I get a crash
>> and the *same* gfortran option is now not recognized:
>>
>> maqroll[felipe_fortran]> f2py -c --fcompiler=gfortran text2.pyf
>> Unknown vendor: "gfortran"
>
> It's --fcompiler=gnu95, not --fcompiler=gfortran

The funny thing is that 'gfortran' does work for the plain call

f2py -c --fcompiler=gfortran Text2.f90

just fine, but not for the other forms.  So it's easy to be misled
into thinking that it might actually be the correct call.  Clever
trick to trap the unwary :)

Cheers,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Robert Kern-2
On Fri, Jul 18, 2008 at 18:19, Fernando Perez <[hidden email]> wrote:

> On Fri, Jul 18, 2008 at 3:53 PM, Robert Kern <[hidden email]> wrote:
>
>> You still need "-m text2", I believe.
>
> Right, thanks.  But it still doesn't quite work.  Consider a makefile with
>
> lib: seglib.so
>
> seglib.so: Text2.f90
>        f2py -c --fcompiler=gnu95 -m seglib Text2.f90
>
> pyf: Text2.f90
>        f2py -h seglib.pyf -m seglib Text2.f90 --overwrite-signature
>
> lib2: Text2.f90
>        f2py -c --fcompiler=gnu95 seglib.pyf

You still need to have Text2.f90 on the line.


lib2: Text2.f90
       f2py -c --fcompiler=gnu95 seglib.pyf Text2.f90


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Fernando Perez
On Fri, Jul 18, 2008 at 4:26 PM, Robert Kern <[hidden email]> wrote:

> On Fri, Jul 18, 2008 at 18:19, Fernando Perez <[hidden email]> wrote:
>> On Fri, Jul 18, 2008 at 3:53 PM, Robert Kern <[hidden email]> wrote:
>>
>>> You still need "-m text2", I believe.
>>
>> Right, thanks.  But it still doesn't quite work.  Consider a makefile with
>>
>> lib: seglib.so
>>
>> seglib.so: Text2.f90
>>        f2py -c --fcompiler=gnu95 -m seglib Text2.f90
>>
>> pyf: Text2.f90
>>        f2py -h seglib.pyf -m seglib Text2.f90 --overwrite-signature
>>
>> lib2: Text2.f90
>>        f2py -c --fcompiler=gnu95 seglib.pyf
>
> You still need to have Text2.f90 on the line.

Ahah!  I went on this:

  -h <filename>    Write signatures of the fortran routines to file <filename>
                   and exit. You can then edit <filename> and use it
***instead***
                   of <fortran files>.

[emphasis mine].  The instead there led me to think that I should NOT
list the fortran files.  Should that docstring be fixed, or am I just
misreading something?

And do you have any ideas on why the f2py_options in setup.py don't
correctly pass the --fcompiler flag down to f2py?  It does work if one
calls setup.py via

./setup.py config_fc --fcompiler=gnu95 build_ext --inplace

but it seems it would be good to be able to set all f2py options
inside the setup.py file (without resorting to sys.argv hacks).  Or
does this go against the grain of numpy.distutils?

Cheers,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Robert Kern-2
On Fri, Jul 18, 2008 at 18:35, Fernando Perez <[hidden email]> wrote:

> On Fri, Jul 18, 2008 at 4:26 PM, Robert Kern <[hidden email]> wrote:
>> On Fri, Jul 18, 2008 at 18:19, Fernando Perez <[hidden email]> wrote:
>>> On Fri, Jul 18, 2008 at 3:53 PM, Robert Kern <[hidden email]> wrote:
>>>
>>>> You still need "-m text2", I believe.
>>>
>>> Right, thanks.  But it still doesn't quite work.  Consider a makefile with
>>>
>>> lib: seglib.so
>>>
>>> seglib.so: Text2.f90
>>>        f2py -c --fcompiler=gnu95 -m seglib Text2.f90
>>>
>>> pyf: Text2.f90
>>>        f2py -h seglib.pyf -m seglib Text2.f90 --overwrite-signature
>>>
>>> lib2: Text2.f90
>>>        f2py -c --fcompiler=gnu95 seglib.pyf
>>
>> You still need to have Text2.f90 on the line.
>
> Ahah!  I went on this:
>
>  -h <filename>    Write signatures of the fortran routines to file <filename>
>                   and exit. You can then edit <filename> and use it
> ***instead***
>                   of <fortran files>.
>
> [emphasis mine].  The instead there led me to think that I should NOT
> list the fortran files.  Should that docstring be fixed, or am I just
> misreading something?
>
> And do you have any ideas on why the f2py_options in setup.py don't
> correctly pass the --fcompiler flag down to f2py?  It does work if one
> calls setup.py via
>
> ./setup.py config_fc --fcompiler=gnu95 build_ext --inplace
>
> but it seems it would be good to be able to set all f2py options
> inside the setup.py file (without resorting to sys.argv hacks).  Or
> does this go against the grain of numpy.distutils?

For publicly distributed packages, it does go against the grain. Never
hard-code the compiler! Use a setup.cfg file, instead.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Fernando Perez
On Fri, Jul 18, 2008 at 4:47 PM, Robert Kern <[hidden email]> wrote:

> For publicly distributed packages, it does go against the grain. Never
> hard-code the compiler! Use a setup.cfg file, instead.

Quite all right.  But this was for in-house code where a group has
agreed to all use the same compiler.  It's basically a matter of
wanting

./setup.py install

to work without further flags.  More generically, the way that
f2py_options work is kind of confusing, since it doesn't just pass
options to f2py :)

In any case, I'm very grateful for all your help, but I get the sense
that all of this distutils/f2py stuff would greatly benefit from
clearer documentation.  I imagine the doc team is already working on
a pretty full plate, but if anyone tackles this particular issue,
they'd be making a very useful contribution.

Cheers,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Robert Kern-2
On Fri, Jul 18, 2008 at 19:04, Fernando Perez <[hidden email]> wrote:

> On Fri, Jul 18, 2008 at 4:47 PM, Robert Kern <[hidden email]> wrote:
>
>> For publicly distributed packages, it does go against the grain. Never
>> hard-code the compiler! Use a setup.cfg file, instead.
>
> Quite all right.  But this was for in-house code where a group has
> agreed to all use the same compiler.  It's basically a matter of
> wanting
>
> ./setup.py install
>
> to work without further flags.

Right. setup.cfg is still the way to go.

> More generically, the way that
> f2py_options work is kind of confusing, since it doesn't just pass
> options to f2py :)

Probably. What other options would you need, though? If everything is
accessible from setup.cfg, I'd rather just remove the argument.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Fernando Perez
On Fri, Jul 18, 2008 at 5:15 PM, Robert Kern <[hidden email]> wrote:

> Probably. What other options would you need, though? If everything is
> accessible from setup.cfg, I'd rather just remove the argument.

I'd be OK with that too, and simply telling people to ship a companion
setup.cfg.  It's just that the current situation is confusing and
error-prone.  One way to do it and all that...

Cheers,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: f2py - how to use .pyf files?

Fernando Perez
On Fri, Jul 18, 2008 at 5:20 PM, Fernando Perez <[hidden email]> wrote:
> On Fri, Jul 18, 2008 at 5:15 PM, Robert Kern <[hidden email]> wrote:
>
>> Probably. What other options would you need, though? If everything is
>> accessible from setup.cfg, I'd rather just remove the argument.

I just remembered code I had with things like:

                    # Add '--debug-capi' for verbose debugging of low-level
                    # calls
                    #f2py_options = ['--debug-capi'],

that can be very useful when in debug hell.  Would this go through via
setup.cfg?

> I'd be OK with that too, and simply telling people to ship a companion
> setup.cfg.  It's just that the current situation is confusing and
> error-prone.  One way to do it and all that...

BTW, I would have thought this file

maqroll[felipe_fortran]> cat setup.cfg
[config_fc]
fcompiler = gnu95

would do the trick.  But it doesn't seem to:

maqroll[felipe_fortran]> ./setup.py build_ext --inplace
...
gnu: no Fortran 90 compiler found
customize GnuFCompiler using build_ext
building 'seglib' extension
...
error: f90 not supported by GnuFCompiler needed for Text2.f90


I'm sure I'm doing something blindingly wrong, but reading the distutils docs

http://docs.python.org/inst/config-syntax.html

made me think that the way to override the config_fc option is
precisely the above.

Thanks for any help...

All this would be good to include in the end in a little example we ship...

Cheers,

f
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Loading...