2

I'm using f2py to compile a Fortran subroutine to be called in a Python script.

I have compiled the Fortran source on Mac #1, running Mojave 10.14. I compiled it using:

f2py -c -m <ModuleName> <SourceName.f90>

This works. I get a .so file that I can them import in Python using:

import <ModuleName> as m

The issue comes when I try to run it on another Mac.

On Mac #2, also running Mojave 10.14, when I try and use the pre-compiled module:

>>> import <ModuleName> as m
Traceback (most recent call last):
  File "Stats_Wizard.py", line 20, in <module>
    import <ModuleName> as sf
ImportError: dlopen(/path/to/<ModuleName>.so, 2): Library not loaded: /usr/local/opt/gcc/lib/gcc/9/libgfortran.5.dylib
  Referenced from: /path/to/<ModuleName>.so
  Reason: image not found

I checked the hash of the .so files on each computer and they matched. Which means there's an issue with using the .so between computers.

Is there a reason why I would not be able to use the same pre-compiled version on each computer?

EDIT: A point of note that differs between Mac #1 and Mac #2: Mac #1 has gfortran installed, Mac #2 does NOT. However, I feel that this should not make a difference because the module is already compiled.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
blackbrandt
  • 2,010
  • 1
  • 15
  • 32

2 Answers2

2

The error (which I believe is self explanatory): Library not loaded: /usr/local/opt/gcc/lib/gcc/9/libgfortran.5.dylib ... Reason: image not found, and the edit at the end of your question already include an (unwanted) answer :)

Your module (ModuleName.so) only contains your code and is using "stuff" from libgfortran.dylib which contains the Fortran core (or backend, if you will).
When your module was built, it was linked to libgfortran.dylib, and as a consequence, it needs that at runtime (when it's loaded). To test that, try otool -L ModuleName.so.

So, libgfortran.dylib needs to be present on any machine (at the specified location) where your module is imported. Since I'm not an OSX user, here are some (theoretical) approaches to make that happen:

  • Install the package that contains it. [GNU.GCC]: gfortran installer for Mac OS X and other URLs mention something about installing GCC. But that seems a too big of an overhead, I wonder if there isn't some lightweight package that contains it too, and install that on the target machines
  • Distribute the file yourself (note that this also applies to any other "special" .dylibs that it depends on). Copy it manually when you install your module. This is a workaround, don't know how feasible would this be from a licensing perspective, plus you also need sudo
  • Build your module against a static libgfortran version. Again, I don't know if there is such a thing (or whether you'd have to build it manually, which I assume that would generate a huge amount of headaches - I built GCC (without Fortran, I think) on OSX, and it took me a couple of days (and lot of research) to get it done)
CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

Please check if you have added directory of your built file to your python path. For checking if this is really the case try:

import sys
sys.path.append(r"dir_parh/to/your/.so/file")
import lib_name
Daniel
  • 980
  • 9
  • 20
  • Thank you. I am importing the module from the same directory. I can import a different file from the same directory fine. – blackbrandt Nov 10 '19 at 22:19
  • I stumbled on this: https://stackoverflow.com/questions/24993752/os-x-framework-library-not-loaded-image-not-found . Have you tried this? I don't have any experience on Mac OS so I can't really tell what's it about, just went for `image not found`. – Daniel Nov 10 '19 at 22:26
  • Thanks for the suggestion, but we solved the problem above. Turns out that it needs a certain `.dylib` file installed in a certain place. – blackbrandt Nov 12 '19 at 03:38