Eiq
Eiq

Reputation: 45

import magic throws exception

So I used pip to download filemagic:

E:\cuckoo-current\cuckoo>c:\Python27\Scripts\pip.exe install filemagic
Downloading/unpacking filemagic
  Downloading filemagic-1.6.tar.gz
  Running setup.py (path:c:\users\vaclav~1.vas\appdata\local\temp\pip_build_vaclav.vasenka\filemagic\setup.py) egg_info for package filemagic

Installing collected packages: filemagic
  Running setup.py install for filemagic

Successfully installed filemagic
Cleaning up...

I've created test.py with content:

import magic
print "test"

And when I run the file import throws exception:

Traceback (most recent call last):
  File "E:\sand\test.py", line 2, in <module>
    import magic
  File "C:\Python27\lib\site-packages\magic\__init__.py", line 18, in <module>
    from magic.identify import Magic, MagicError
  File "C:\Python27\lib\site-packages\magic\identify.py", line 16, in <module>
    from magic import api
  File "C:\Python27\lib\site-packages\magic\api.py", line 22, in <module>
    raise ImportError('Unable to find magic library')
ImportError: Unable to find magic library

Is the filemagic corrupted ? Is this module even working ?

Upvotes: 1

Views: 3895

Answers (2)

Prashant Garg
Prashant Garg

Reputation: 1

I faced the same problem with docker containers with Alpine Linux image:

It seems like in Alpine Linux the library search doesn't work, hence it fails to find 'libmagic.so.1' which is required for the working of magic library( dependency filemagic and libmagic).

Workaround:

  1. Use Python slim instead of Alpine Linux
  2. mime, mimetypes are the other python libraries which can be used (the only downside to these two libraries is, that they find the mime type based on file extension and not file headers.

Upvotes: 0

Tritium21
Tritium21

Reputation: 2932

From the PYPI Description (emphasis mine)

A Python API for libmagic, the library behind the Unix file command

and

libmagic is the library that commonly supports the file command on Unix system, other than Max OSX which has its own implementation. The library handles the loading of database files that describe the magic numbers used to identify various file types, as well as the associated mime types. The library also handles character set detections.

You cannot use this on Windows, as you are attempting.

You can attempt to use the mimetypes module in the standard library (mimetypes.guess_type would be what to look at).

Upvotes: 3

Related Questions