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)?
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:
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.