pythonmacoslibmagic

Can't install libmagic on Mac M1 Pro


I am having significant trouble installing some Python libraries/dependencies for a project I have to work on, specifically with libmagic. I have tried to install libmagic several ways with no success - installing it through my code editor terminal, installing it through terminal with pip, installing it via Homebrew (including removing Python and existing items and re-installing everything with homebrew).

File "/Users/bob/.pyenv/versions/3.6.15/envs/atw/lib/python3.6/site-packages/magic.py", line 181, in <module>
raise ImportError('failed to find libmagic.  Check your installation') ImportError: failed to find libmagic.  Check your installation

Any suggestions on how to resolve this?


Solution

  • Resolved by following this answer by jhavej from Github:

    In my case (M1 MacBook), it was all about telling the system where to find the library files. So after obvious brew install libmagic (on M1s, homebrew installs libmagic to /opt/homebrew/Cellar/libmagic), it was necessary to create a symbolic link from the library files to the place where the system expects libmagic to be located, which is /usr/local/lib.

    Therefore, this worked for me:

    sudo ln -s /opt/homebrew/Cellar/libmagic/5.41/lib/libmagic.dylib /usr/local/lib/libmagic.dylib
    

    FYI: my version of python-magic is 0.4.18 (and I wasn't willing to downgrade as suggested above)