ImportError: failed to find libmagic. Check your installation

本文描述了在MacHighSierra上安装Python-magic遇到的libmagic问题,包括错误处理、依赖关系、使用Homebrew解决以及创建软链接的过程,确保Python-magic能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Installing libmagic in Mac OS (for Python-magic)

When trying to install python-magic I found out that libmagic was not installed (or not properly available) in Mac High Sierra. There was little information on this problem on the Internet so I thought it might be helpful if I briefly wrote down my experiences.

When installing python-magic:

pip install python-magic

there was an error reported by ctypes (a depency that is installed as well). As ctypes should be part of your python (since 2.5) you can ignore this error or install python-magic from this updated version on GitHub: https://github.com/ahupp/python-magic

After the installation completes however, python-magic gives an error (libmagic not available):

>>> import magic

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "[...]/python-magic/magic.py", line 128, in <module>

raise ImportError('failed to find libmagic. Check your installation')

ImportError: failed to find libmagic. Check your installation

>>>

The easiest way to install libmagic is through Homebrew, a Mac package manager. I used to resort to Fink or Macports in the past, but Brew seems a lot cleaner and more straightforward. Install Homebrew according to the instructions and then install libmagic:

brew install libmagic

Brew installs libmagic in /usr/local/Cellar/ . Python-magic however seems to be looking for a reference to libmagic in /usr/local/lib/

To solve this last problem we can create a symlink in /usr/local/lib to the right file.

cd /usr/local/lib/

ln -s ../Cellar/libmagic/5.04/lib/libmagic.dylib libmagic.dylib

After this, python-magic is able to find libmagic and works properly.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值