ImportError: dlopen (Apple Silicon)

Viewed 1160

I am new to python, I was trying to import some libraries.

import sys
import cv2
import face_recognition
import pickle

But, I get this error in VS Code. When I run the same thing on PyCharm CE, It doesn't even recognize the module cv2. I know, I have installed opencv-python, but the issue still persists.

Traceback (most recent call last):
  File "/Users/vedantamohapatra/Downloads/Face Detection/embedding.py", line 3, in <module>
    import face_recognition
  File "/opt/homebrew/lib/python3.9/site-packages/face_recognition/__init__.py", line 7, in <module>
    from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance
  File "/opt/homebrew/lib/python3.9/site-packages/face_recognition/api.py", line 4, in <module>
    import dlib
  File "/opt/homebrew/lib/python3.9/site-packages/dlib/__init__.py", line 19, in <module>
    from _dlib_pybind11 import *
ImportError: dlopen(/opt/homebrew/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so, 0x0002): tried: '/opt/homebrew/lib/python3.9/site-packages/_dlib_pybind11.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/_dlib_pybind11.cpython-39-darwin.so' (no such file), '/usr/lib/_dlib_pybind11.cpython-39-darwin.so' (no such file)

I tried searching for this error but didn't find any working solution. Is this something which can't be solved now due to the architecture? As I got a (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')) error in there too. I am using M1 Mac Monterey 12.1. On a side node, If this issue is unresolved, can anyone please point out any alternative for this, like would Google Colab be fine?

1 Answers

I am not sure if this would work for your system too but this is what worked for me. I had PyCharm installed so I used its default Python Package installer located at the bottom left of the window and then added the required packages. This method not only solved the issue of the code not running in PyCharm but my code ran in the terminal too.

I think this might be due to conflicting Python versions installed in the Mac and the Terminal Package installer is installing it in different directory that which is used by the compiler during execution. This might be due to improper transition from Python 2 to Python 3 in Mac I feel.

If I find a more general fix to this I will update the answer.

Related