Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: How can I get F2PY working on Apple M1?

I want to import fortran files to my python script by using f2py.

For that I compile them via

f2py -c -m my_lib *.f

which produces the file "my_lib.cpython-38-darwin.so" which I import to my script by

import my_lib.

On my Intel-based Macbook that works well. However, running the script on an M1 machine yields the following error:

ImportError: dlopen(./my_lib.cpython-38-darwin.so, 0x0002): tried: './my_lib.cpython-38-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/local/lib/my_lib.cpython-38-darwin.so' (no such file), '/usr/lib/my_lib.cpython-38-darwin.so' (no such file)

Same happens when I start my terminal in Rosetta mode.

Any idea how to solve this issue?

like image 713
stegmaja Avatar asked Sep 19 '25 13:09

stegmaja


1 Answers

I don't know if you could solve the problem but I found a solution to solve it on my Mac.

Running f2py on my Mac caused the same error. After some time of searching for reasons I tried to use another Python Environment (based on another Base Interpreter).
Till changing I used an Anaconda Environment.

I think the error could be caused by an interpreter that's not running natively on the M1 chip. The compiled Fortran code must run natively on the Mac. So probably there could be problems in compatibility.

Now I'm using the Interpreter given by the "Developer Command Line Tools" (in this case Python 3.8) to compile the source code and to build the .so file.
I also use this Base Interpreter to run my script that has to include the Fortran package.

By doing so I was able to make it work on my Mac.

like image 126
rnoffke Avatar answered Sep 21 '25 04:09

rnoffke