Integrating Python with Fortran

"Martin v. Löwis" martin at v.loewis.de
Tue Oct 31 16:33:57 EST 2006


unexpected schrieb:
> 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



More information about the Python-list mailing list