Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent ImportError: failed to find libmagic #238

Closed
fsboehme opened this issue Mar 2, 2021 · 8 comments · May be fixed by #294
Closed

Persistent ImportError: failed to find libmagic #238

fsboehme opened this issue Mar 2, 2021 · 8 comments · May be fixed by #294

Comments

@fsboehme
Copy link

fsboehme commented Mar 2, 2021

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
@cameron-emburse
Copy link

cameron-emburse commented Mar 2, 2021

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

@fsboehme
Copy link
Author

fsboehme commented Mar 2, 2021

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?

@fsboehme
Copy link
Author

fsboehme commented Mar 2, 2021

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

@cameron-emburse
Copy link

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...

@fsboehme
Copy link
Author

fsboehme commented Mar 2, 2021

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!

@fsboehme fsboehme closed this as completed Mar 2, 2021
@cameron-emburse
Copy link

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

@anentropic
Copy link

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

@anentropic
Copy link

anentropic commented Sep 7, 2021

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants