0

I have some old fortran code that I am wrapping and importing to python using f2py. The fortran code relies on a data file that resides in the same directory. If I start python in that directory, everything works fine.

However, if I import this module from somewhere else, it looks for the files locally and obviously cannot find them.

Is there a way to tell the module where to execute the fortran code (or another clever way around it)?

John Smith
  • 1,059
  • 1
  • 13
  • 35

1 Answers1

0

I don't know a lot about f2py but it looks like there is a command line version and a module version.

To use the commmand line option, I see two options:

  1. Modify the fortran to find the path
  2. Wrap the command line with a wrapper

For a wrapper, you could use a bash script that does something like this:

#!/bin/sh
dir=$(dirname $0)       # gets relative path
dir=$(readlink -f $dir) # to get absolute
cd $dir
# Now call p2py

If you use the module version, you can use os.chdir() to change the directory before running your pythonized fortran source:

fortran_file = "foo.f"
dir = os.path.dirname(os.path.abspath(fortran_file))
os.chdir(dir)
# Now run p2py against fortran_file

Only other option I can think of is to see if you can inject the path into the fortran code. Maybe you can read the code, change the path in the source in memory, and then use f2py.compile() against that dynamically modified code.

rrauenza
  • 6,285
  • 4
  • 32
  • 57
  • ok, but for this I would need to modify that fortran code to read from a specified path and then write a python wrapper that calls that fortran-module, right? There is no (simpler) way of telling the f2py module to run where it resides? – John Smith Jun 20 '16 at 14:17
  • @JohnSmith ok, read up on `f2py` to get more context. Updated answer. – rrauenza Jun 20 '16 at 16:10
  • thanks, the wrapper that changes to the path of the module is probably the easiest, I'll go with this. – John Smith Jun 21 '16 at 07:22