views:

151

answers:

2

Hello,

Although I found many answers and discussions about this question, I am unable to find a solution particular to my situation. Here it is:

I have a main program written in FORTRAN. I have been given a set of python scripts that are very useful. My goal is to access these python scripts from my main FORTRAN program. Currently, I simply call the scripts from FORTRAN as such:

CALL SYSTEM ('python pyexample.py')

Data is read from .dat files and written to .dat files. This is how the python scripts and the main FORTRAN program communicate to each other.

I am currently running my code on my local machine. I have python installed with numpy, scipy, etc.

My problem: The code needs to run on a remote server. For strictly FORTRAN code, I compile the code locally and send the executable to the server where it waits in a queue. However, the server does not have python installed. The server is being used as a number crunching station between universities and industry. Installing python along with the necessary modules on the server is not an option. This means that my “CALL SYSTEM ('python pyexample.py')” strategy no longer works.

Solution?: I found some information on a couple of things in thread http://stackoverflow.com/questions/138521/is-it-feasible-to-compile-python-to-machine-code

Shedskin, Psyco, Cython, Pypy, Cpython API

These “modules”(? Not sure if that's what to call them) seem to compile python script to C code or C++. Apparently not all python features can be translated to C. As well, some of these appear to be experimental. Is it possible to compile my python scripts with my FORTRAN code? There exists f2py which converts FORTRAN code to python, but it doesn't work the other way around.

Any help would be greatly appreciated. Thank you for your time.

Vincent

PS: I'm using python 2.6 on Ubuntu

A: 

You don't want any of those. What you should do is use FORTRAN's FFI to talk with libpython and frob its API.

Ignacio Vazquez-Abrams
+1  A: 

One way or another, you'll need to get the Python runtime on your server, otherwise it won't be possible to execute Python bytecode. Ignacio is on the right track with suggesting invoking libpython directly, but due to Fortran's parameter-passing conventions, it will be a lot easier for you to write a C wrapper to handle the interface between Fortran and the CPython embedding API.

Unfortunately, you're doing this the hard way -- it's a lot easier to write a Python program that can call Fortran subroutines than the other way around.

Daniel Pryden