Integrating Python with Fortran

U

unexpected

Hi all,

I'm currently working on a large, legacy Fortran application. I would
like to start new development in Python (as it is mainly I/O related).
In order to do so, however, the whole project needs to be able to
compile in Fortran.

I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.

I realize that all of this is highly dependent on the libraries I use,
etc, but I'm just looking for general strategies to attack the problem
or someone to tell me that this is impossible.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

unexpected said:
I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.

I realize that all of this is highly dependent on the libraries I use,
etc, but I'm just looking for general strategies to attack the problem
or someone to tell me that this is impossible.

Please take a look at the "extending and embedding" tutorial. This
explains you how to integrate Python code into a C application.
If you think this could work for you if just your application was C, I
think the Python-Fortran people can give you precise instructions on how
to integrate Python code into a Fortran program.

In the simplest embedding example, you just link the Python VM itself
into the hosting application. The actual Python files stay on disk, and
invoking a Python function from C/Fortran will just end up doing a
regular Python import (with searching sys.path and everything).

If, for packaging reasons, you prefer to have the Fortran program
stand-alone, I recommend to use freeze. freeze gives you indeed .o files
for Python files, plus a global table of all frozen modules. Then, when
doing an import, the interpreter won't go to disk anymore, but it will
import the byte code "from memory" (it still would be a regular import
operation). Freeze, of course, requires you to recompile/relink your
application every time you change a Python source file.

HTH,
Martin
 
R

Robert Kern

unexpected said:
Hi all,

I'm currently working on a large, legacy Fortran application. I would
like to start new development in Python (as it is mainly I/O related).
In order to do so, however, the whole project needs to be able to
compile in Fortran.

I'm aware of resources like the F2Py Interface generator, but this only
lets me access the Fortran modules I need in Python. I'm wondering if
there's a way to generate the .o files from Python (maybe using
py2exe?) and then link the .o file with the rest of the Fortran project
using something like gcc.

There are no tools specifically for embedded Python in Fortran. However, there
is Elmer, which helps to create a C API for a Python library (essentially the
inverse of SWIG). One can then wrap that C API with a Fortran-compliant
interface in the fairly standard ways.

http://elmer.sourceforge.net/

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top