Python import from subfolder -- Fortran .so inside .so from f2py -- ImportError (Image not found)

255 Views Asked by At

I am importing a Python library 'Interface' located in a subfolder 'Fortran_Interface' (wrapped with f2py):

import Interface

'Interface.so' was compiled to link to the Fortran lib 'libsolver.so'. The compilation was done with the commands

gfortran -shared -O2 -o libsolver.so -fPIC Solver/Module_Solver.f90
f2py -c --fcompiler=gfortran -L. -I. -lsolver -m Interface Interface/Module_Interface.f90

I get the error

  ImportError: dlopen(/Users/gmueller/Workspace/PySpinX_G/Fortran_Interface/Interface.so, 2): Library not loaded: libsolver.so
  Referenced from: /Users/gmueller/Workspace/PySpinX_G/Fortran_Interface/Interface.so
  Reason: image not found

I can import the library from inside the library's directory, but not from the folder above. This seems to me to be because 'Interface.so' linked with a relative path to 'libsolver.so', which python uses from the directory above.

I'd be thankful for any ideas on how to solve this. Compiling the .so's with absolute paths did not work, as well as calling an python script that is in the library directory from my main script...

Edit: with a symbolic link from my python script's folder to the lib solver.so, it works.

1

There are 1 best solutions below

0
On

(Question resolved by the OP and edited in a solution. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

with a symbolic link from my python script's folder to the lib solver.so, it works.