bbopt / nomad

NOMAD - A blackbox optimization software
https://nomad-4-user-guide.readthedocs.io/
GNU Lesser General Public License v3.0
116 stars 24 forks source link

Python importError when building PyNomad interface for Apple M1 chip (apple ARM64 or intel X86_64) #70

Open ctribes opened 2 years ago

ctribes commented 2 years ago

When using Clang compiler with OSX on Apple M1 chip, after building PyNomad successfully, when importing PyNomad in Python

import PyNomad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/christophe/.local/lib/python3.9/site-packages/PyNomad.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '__ZN9NOMAD_4_210Parameters17_typeOfAttributesE'

or

ImportError: dlopen(xxxx/PyNomad.cpython-39-darwin.so, 0x0002) .... 
(mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))

This issue can be resolved by forcing the architecture to X86_64 when configuring the cmake build with a flag like -DCMAKE_OSX_ARCHITECTURES=x86_64. This is required when Python binaries for the API are x86_64. This can be tested with the command file. For example,

>> file /opt/homebrew/anaconda3/bin/python
/opt/homebrew/anaconda3/bin/python: Mach-O 64-bit executable x86_64

In this case, the nomad binaries must all be x86_64, which is not the default when building with Clang.

It is important, to remove the build directory and the PyNomad.cpython-39-darwin.so in $NOMAD_HOME/interfaces/PyNomad to make sure all PyNomad binaries are deleted before building again.