[Numpy-discussion] Help using numpy.distutils.fcompiler for my GSoC project

Kurt Smith kwmsmith at gmail.com
Tue Jun 23 15:48:18 EDT 2009

On Tue, Jun 23, 2009 at 1:41 PM, Lisandro Dalcin<dalcinl at gmail.com> wrote:
> On Mon, Jun 22, 2009 at 11:53 PM, Kurt Smith<kwmsmith at gmail.com> wrote:
>> Hello,
>> Is there a way for numpy.distutils to compile a fortran source file
>> into an executable?
> If the whole point of building the executable is to run it in order to
> parse the output, then you can start with this:
> $ cat setup.py
> from numpy.distutils.core import setup
> from numpy.distutils.command.config import config as config_orig
> helloworld = """
> program hello
>    write(*,*) "Hello, World!"
> end program hello
> """
> class config(config_orig):
>    def run(self):
>        self.try_run(helloworld, lang='f90')
> setup(name="ConfigF90",
>      cmdclass={'config' : config})
> $ python setup.py config
> <... lots of ouput ...>
> gfortran:f90: _configtest.f90
> /usr/bin/gfortran -Wall -Wall _configtest.o -lgfortran -o _configtest
> _configtest
>  Hello, World!
> success!
> removing: _configtest.f90 _configtest.o _configtest

Thank you!

The comments at the top of numpy.distutils.command.config.py make me
think that try_run won't work for all fortran compilers on all
platforms.  But this certainly helps and I've almost got it solved.

What I've discovered is something more low-level, like the following:

$ cat batch.py
from numpy.distutils.fcompiler import new_fcompiler

fcomp = new_fcompiler() # will grab the default fcompiler, overridable
with keyword args.

objs = fcomp.compile(['./genconfig.f95']) # compiles into an object.

fcomp.link_executable(objs, 'genconfig') # makes the executable 'genconfig'

# spawn the executable here to generate config files.

The above works for simple cases (but for the spawning step, but I'll
have that working soon) -- I'll probably end up wrapping it in a run
method inside a config subclass, like you have above.

Thanks again,


More information about the NumPy-Discussion mailing list