Reputation: 93
I'm currently developing a script called script.py
that makes use of the libmagic
package. I used homebrew (3.5.9)
to install it via ZSH shell on an M1 Pro Mac.
As with any homebrew package installation, I used brew install libmagic
and it installed successfully, but by the time I tried to import it to my program with import magic
, the ModuleNotFoundError
was thrown.
At first, I thought the module was not found because it was installed as a third party module, so I added the path to $PYTHONPATH
and I verified it via printing each entries with sys.path
. But, I still get the same error.
One assumption I made here is that the path I gave to $PYTHONPATH
is where the magic
module is located, but I think I have done something wrong here because the module search path clearly has the path I included so why is it still throwing the same error?
Clearly, the path listed in $PYTHONPATH
was included as shown below, so why can't the module be found?
I'm suspecting that I'm not using the right directory for my $PYTHONPATH
and most tutorials out there uses an Intel chip which has a different file structure than the one in M1 Pro. The packages installed in the M1 Pro are stored under /opt/homebrew/Cellar
and I might have missed some important steps.
Anybody who has experienced similar problems, please show me the way. Thank you.
Upvotes: 1
Views: 5024
Reputation: 6170
Install libmagic
on Apple Silicon Macs based on M1 or M2 (or Mx):
Step 1) First, try to uninstall any other already-installed libmagic
libraries and then install python-magic-bin
:
$ pip uninstall python-magic
$ pip install python-magic-bin
If it was successful, then you are all set! If that fails, however, that means the wheel (binary) is not found for your Mac, then head to Step 2.
Step 2) Install libmagic
via Homebrew:
$ brew install libmagic
Step 3) Copy the magic
directory from the repository to the directory where your Python environment libraries are located. Run $ pip list -v
to be able to locate the path to your libraries directory. As an explanation on the origin of the magic
directory, it has been derived from an Intel-based Mac with python-magic
installed via pip.
Step 4) Copy libmagic.dylib
from the lib
directory in the libmagic that Homebrew has installed to the magic/libmagic
directory in Step 3 to replace the YOUR_libmagic.dylib
. Please note that you need to copy the original file, not the alias (symbolic link).
Run $ brew list -v
to help you locate the path to the library installed by Homebrew. A typical path looks like /usr/local/Cellar/libmagic/5.44/lib
.
If you have done the steps above, then you are going to be able to import libmagic:
import magic
Upvotes: 4
Reputation: 53
You can download the official version of libmagic module with these commands
> pip install python-magic
or
> pip3 install python-magic ## if you have different versions of python installed
or if u dont have pip installed install it by using these commands
> curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
> python3 get-pip.py
Upvotes: 2