How to compile Python scripts for use in FORTRAN?

Although I have found many answers and discussions on this issue, I cannot find a solution specific to my situation. Here he is:

I have a main program written in FORTRAN. I am provided with a set of python scripts that are very useful. My goal is to access these python scripts from my main FORTRAN program. I am currently just calling scripts from FORTRAN as such:

CALL SYSTEM ('python pyexample.py')

Data is read from .dat files and written to .dat files. This way the python scripts and the main FORTRAN program interact with each other.

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

My problem: The code must run on a remote server. For strictly FORTRAN code, I compile the code locally and send the executable to the server on which it is waiting in line. However, there is no python installed on the server. The server is used as a crystal 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 about several things in the thread. Is it possible to compile Python for machine code?

Shedskin, Psyco, Cython, Pypy, Cpython API

These β€œmodules” (β€œNot sure what to call them”) seem to compile the python script into C code or C ++. Apparently, not all python functions can be translated into C. Also, some of them look experimental. Is it possible to compile my python scripts with my FORTRAN code? There is f2py that converts the 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 am using python 2.6 on Ubuntu

+7
python fortran compilation
source share
2 answers

One way or another, you will need to run the Python runtime on your server, otherwise it will be impossible to execute the Python bytecode. Ignacio is on the right track, offering direct reference to libpython, but due to Fortran parameter passing conventions, it will be much easier for you to write a C wrapper to handle the interface between Fortran and the CPython implementation implementation API.

Unfortunately, you are not doing this easily - it is much easier to write a Python program that can call Fortran routines than vice versa.

+3
source share

You do not want any of them. What you need to do is use FORTRAN FFI to communicate with libpython and frob its API .

+1
source share

All Articles