简体   繁体   中英

Getting error message when attempting to use python-magic to identify file types on windows

I am new to Python with some knowledge of C++. I am getting the following error message when trying to set up python-magic to identify file types. I have already installed python-magic and libmagic using pip. As soon as I try to import magic I get the error message prompting me to check my installation.

Error message:

# python
>>> import magic
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import magic
  File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 173, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation
>>> 

Thanks guys, now I am able to import magic, but I am getting another error message when I try to type magic.from_file("userinput.py") after placing the magic1.dll and libgnurx-0.dll files into my C:\\Windows\\System32 folder. Error message follows:

# python

>>> import magic
>>> magic.from_file("userinput.py")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
magic.from_file("userinput.py")
File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 130, in from_file
m = _get_magic_type(mime)
File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 118, in _get_magic_type
i = _instances[mime] = Magic(mime=mime)
File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 67, in __init__
magic_load(self.cookie, magic_file)
File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 256, in magic_load
return _magic_load(cookie, coerce_filename(filename))
File "C:\Users\adamh\AppData\Local\Programs\Python\Python35\lib\site-packages\magic.py", line 187, in errorcheck_negative_one
raise MagicException(err)
magic.MagicException
>>> 

I think the problem is I am having trouble passing the location of magic.mgc using the following pseudo command: file_magic = magic.Magic(magic_file="c:path\\to\\magic.mgc")

Any further help would be greatly appreciated. Thank you all.

It sounds like you don't have the proper dependencies set up. Have you followed the installation instructions from the github page ?

"On Windows, copy magic1.dll, regex2.dll, and zlib1.dll onto your PATH from the Binaries and Dependencies zipfiles provided by the File for Windows project. You will need to copy the file magic out of [binary-zip]\\share\\misc, and pass it's location to Magic(magic_file=...). If you are using a 64-bit build of python, you'll need 64-bit libmagic binaries which can be found here: https://github.com/pidydx/libmagicwin64 (note: untested)"

From https://github.com/ahupp/python-magic#dependencies :

Dependencies

On Windows, copy magic1.dll, regex2.dll, and zlib1.dll onto your PATH from the Binaries and Dependencies zipfiles provided by the File for Windows project. You will need to copy the file magic out of [binary-zip]\\share\\misc , and pass it's location to Magic(magic_file=...) . If you are using a 64-bit build of python, you'll need 64-bit libmagic binaries which can be found here: https://github.com/pidydx/libmagicwin64 (note: untested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM