ahupp / python-magic

A python wrapper for libmagic
Other
2.6k stars 280 forks source link

Does python-magic work on Apple Silicon? #262

Closed soimkim closed 2 years ago

soimkim commented 2 years ago

When Python-magic is installed and executed on Apple Silicon(m1), the following bug occurs. Is there any workaround?

 ImportError: dlopen(/usr/local/Cellar/libmagic/5.41/lib/libmagic.dylib, 0x0006): tried: '/usr/local/Cellar/libmagic/5.41/lib/libmagic.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/libmagic.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libmagic.dylib' (no such file), '/usr/local/Cellar/libmagic/5.41/lib/libmagic.1.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/libmagic.1.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/lib/libmagic.1.dylib' (no such file)

failed to find libmagic.  Check your installation
ahupp commented 2 years ago

It looks like you're running a native (ARM) python, but python-magic is finding an Intel copy of the libmagic shared library.

Do you know how /usr/local/Cellar/libmagic/5.41/lib/libmagic.dylib got installed? It looks like homebrew now supports M1 officially (just as of a few weeks ago), so could you try get M1-native homebrew packages installed?

Alternatively, you could run python from your current Intel homebrew install instead of (presumably) the OS install, and it would use the runtime translation and presumably work at the cost of a speed hit.

soimkim commented 2 years ago

/usr/local/Cellar/libmagic/5.41/lib/libmagic.dylib was installed with brew install libmagic. Even after re-installing with brew reinstall, the result is the same. 😢

I'll check again if m1-native homebrew is installed. Thanks for the reply. 🙂

wshs0713 commented 2 years ago

Here has a strange solution:

pip3 install python-magic-bin
pip3 uninstall python-magic

But I don't know why it works...

ahupp commented 2 years ago

@wshs0713 not sure why that would work...

@soimkim are you using homebrew 3.0? That's the one with M1 support, and it defaults to /opt/homebrew according to this page: https://mac.install.guide/homebrew/index.html

soimkim commented 2 years ago

When I installed libmagic I installed it wrong. Reinstalled brew and re-installed libmagic and it works fine. 😄 (python-magic-bin did not work in m1, so I installed it as python-magic.)

Thanks for answering my question.👍

HansBambel commented 7 months ago

After running

brew install libmagic
pip install python-magic

I had the issue that my python platform was for x86_64, but brew installed the arm64 binaries of libmagic. This resulted in a hidden error about a platform mismatch (#279).

Check python-platform:

import platform
platform.machine()

I then uninstalled the recently installed arm64-libmagic-binaries and installed another homebrew with x86_64 architecture (this is likely overkill):

# How to install it with x86 libomp:
# install x86_64 variant of brew 
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# install x86_64 variant of libmagic
arch -x86_64 /usr/local/bin/brew install libmagic

The alternative would have been to set up the conda environment as arm64 (probably easier): https://stackoverflow.com/questions/65415996/how-to-specify-the-architecture-or-platform-for-a-new-conda-environment-apple