Skip to content

Persistent ImportError: failed to find libmagic #238

Closed

Description

@fsboehme

I tried installing libmagic with both Homebrew and Macports (on a new Macbook Air M1 2020), but I'm unable to get it to work. What am I missing?

python-magic==0.4.22

import magic
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../venv/lib/python2.7/site-packages/magic/__init__.py", line 210, in <module>
    libmagic = loader.load_lib()
  File ".../venv/lib/python2.7/site-packages/magic/loader.py", line 39, in load_lib
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation
% man libmagic

LIBMAGIC(3)              BSD Library Functions Manual              LIBMAGIC(3)
...
BSD                              June 8, 2019                              BSD

Activity

  1. cameron-emburse commented on Mar 2, 2021

    @cameron-emburse

    Seems like homebrew has changed to /opt/homebrew/Cellar instead of /usr/local/Cellar/ where this package is looking.

  2. fsboehme commented on Mar 2, 2021

    @fsboehme
    Author

    Thanks for the quick reply! Combining your answer and this article I found earlier, I've created a symlink here:

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

    (I first tried only creating a symlink from /usr/local/Cellar/ to /opt/homebrew/Cellar but that didn't seem to do anything)

    Now I'm getting

    >>> import magic
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../venv/lib/python2.7/site-packages/magic/__init__.py", line 210, in <module>
        libmagic = loader.load_lib()
      File ".../venv/lib/python2.7/site-packages/magic/loader.py", line 16, in load_lib
        libmagic = ctypes.CDLL(dll)
      File ".../opt/anaconda3/envs/py2/lib/python2.7/ctypes/__init__.py", line 366, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: dlopen(/usr/local/lib/libmagic.dylib, 6): no suitable image found.  Did find:
            /usr/local/lib/libmagic.dylib: mach-o, but wrong architecture
            /opt/homebrew/Cellar/libmagic/5.39/lib/libmagic.1.dylib: mach-o, but wrong architecture
    

    any ideas?

  3. fsboehme commented on Mar 2, 2021

    @fsboehme
    Author

    I'm guessing this is something. But no idea how to rectify...

    % lipo -info /opt/homebrew/Cellar/libmagic/5.39/lib/libmagic.1.dylib                
    Non-fat file: /opt/homebrew/Cellar/libmagic/5.39/lib/libmagic.1.dylib is architecture: arm64
    
    % lipo -info ******/venv/bin/python
    Non-fat file: /Users/felixboehme/PycharmProjects/leaguelobster/mysite/venv/bin/python is architecture: x86_64
    
  4. cameron-emburse commented on Mar 2, 2021

    @cameron-emburse

    To be honest, I just modified the python-magic to in my venv point to /opt/homebrew/Cellar/libmagic/*/lib/libmagic.dylib in addition to /usr/local/Cellar/libmagic/*/lib/libmagic.dylib for now...

  5. fsboehme commented on Mar 2, 2021

    @fsboehme
    Author

    Figured it out. The problem was that, due to compatibility issues with python2 on the M1 macbooks, I've had to install python2 via anaconda, which uses x86_64 architecture. Then I installed libmagic via Homebrew with arm64 architecture.

    So the solution was:

    1. Install libmagic through anaconda via conda-forge
    conda install -c conda-forge libmagic
    
    1. Then find your anaconda packages folder (search for the pkgs folder) and the libmagic folder within that and create a symlink in usr/local/lib to that libmagic.dylib. Mine looked like this:
    cd /usr/local/lib
    sudo ln -s /Users/******/opt/anaconda3/pkgs/libmagic-5.39-haaf19a9_0/lib/libmagic.dylib libmagic.dylib
    
    1. import magic works!
  6. cameron-emburse commented on Mar 3, 2021

    @cameron-emburse

    FWIW, I think this is still an issue for 3.whatever apple m1 supports (3.9 in my case)

  7. anentropic commented on Sep 7, 2021

    @anentropic

    this error is still affecting anyone installing on arm mac due to:

    ] + glob.glob('/usr/local/Cellar/libmagic/*/lib')

    and:
    https://docs.brew.sh/FAQ#why-should-i-install-homebrew-in-the-default-location

    The default prefix is /usr/local for macOS on Intel, /opt/homebrew for macOS on Apple Silicon/ARM, and /home/linuxbrew/.linuxbrew for Linux

    I guess could be fixed just by adding extra default search paths

    another option could be to detect the platform and parse the actual cellar path from output of brew info --json libmagic

  8. anentropic commented on Sep 7, 2021

    @anentropic

    ignore me... '/opt/homebrew/lib' is searched in current version of python-magic and my homebrew has installed a libmagic.dylib symlink there already

    the problem is actually my project is using older version of python-magic 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions