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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容