Missing files for `magic` library on Windows
Asked Answered
C

5

10

I need to get mime type for some files on windows, so i've installed python-magic (on 32-bit python 2.7.3).
It depends on unix magic library.
Author instructs to get regex2.dll, zlib1.dll and magic1.dll from gnuwin32 project. So i saved the files to a folder and added the folder to my system PATH.
Now when i execute magic methods, i get missing file exception:

import magic
print(magic.Magic())

Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 367, in <module>
  test_magic()
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 364, in test_magic
  print(magic.Magic())
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 52, in __init__
  magic_load(self.cookie, magic_file)
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 188, in magic_load
  return _magic_load(cookie, coerce_filename(filename))
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 139, in errorcheck
  raise MagicException(err)
magic.MagicException: could not find any magic files!

DLLs are in the PATH, i tried debugging and magic1.dll is located correctly, but somewhere inside library throws an exception.
Inside the gnuwin32 package i've found magic and magic.mgc. I placed them to the same folder, and got WindowsError: [Error 126] on

libmagic = None  
# Let's try to find magic or magic1  
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')  

# This is necessary because find_library returns None if it doesn't find the library
if dll:
    libmagic = ctypes.CDLL(dll)

This obviously happens because python tries to open magic file as dll, which is plain text. After adding .dll to filenames in the code i get the same magic.MagicException: could not find any magic files!.
What files am i missing?

UPDATE:

C:\Users\Admin>file C:\123.zip -m magic
file: could not find any magic files!

C:\Users\Admin>file C:\123.zip -m "C:\@DEV\@LIB\@Magic\GetGnuWin32\bin\magic"
C:\123.zip; ASCII text, with no line terminators

C:\Users\Admin>cd C:\@DEV\@LIB\@Magic\GetGnuWin32\bin

C:\@DEV\@LIB\@Magic\GetGnuWin32\bin>file C:\123.zip -m magic
C:\123.zip; ASCII text, with no line terminators

UPDATE 2 (SOLVED):

print(magic.Magic())
magic.MagicException: could not find any magic files!

print(magic.Magic(magic_file = 'magic'))
<magic.Magic instance at 0x02A5E198>

just had to specify file explicitly

Camillecamilo answered 7/2, 2013 at 22:8 Comment(8)
Stupid question, have you restarted the command prompt AFTER you've added the DLL's to the PATH and BEFORE you executed the script? (silly question, since you obviously know your way around ctypes)Hemorrhoidectomy
"have you restarted the command prompt AFTER you've added the DLL's to the PATH" - yes, i didCamillecamilo
Tried placing the dll's in the same directory and try ctypes on those? or if that might help to begin with? I don't know if they're looking for the DLL's at a specific place such as ./ but it might be good just to try? - Crap, just re-read the question.. you already did.. (i'm tired, 00:00 AM here)Hemorrhoidectomy
Have tried placing in the same directory with my script - all the same.Camillecamilo
Does this help: bytes.com/topic/python/answers/… (specificly the part about the folder names in which the DLL's should be placed)Hemorrhoidectomy
I've tried share, share/file, file, file/share, file/share/file and c:/program files/file/share/file/ directories. None of it helped.Camillecamilo
Tried using the -m param on Python when starting the script and get the module import type and a better verbosity of what's happening? Python -h for more param options but they can give you a better trace of what it's trying to import and where, hopefully you can corelate that with why the DLL's appear to be missing..Hemorrhoidectomy
Please answer your question, if I am following your last update you were missing the actual "magic" file/lookup table of mime-types to suffixs.Unveil
C
2

Path to magic file has to be explicitly passed to the constructor.

magic_object = magic.Magic(magic_file = 'path_to_magic_files/magic'))
Camillecamilo answered 21/2, 2013 at 16:26 Comment(1)
@picomon perfect questions. people with limited communication skills tend to forget mentioning such detailsPigsty
S
11

For future google visitors: Another solution is setting the %MAGIC% enviroment variable in the systems setting to point to the magic file, for me it was:

"c:\Program Files (x86)\GnuWin32\share\misc\magic"

No need to hardcode the path in your program!

Spunk answered 30/1, 2015 at 11:13 Comment(0)
C
2

Path to magic file has to be explicitly passed to the constructor.

magic_object = magic.Magic(magic_file = 'path_to_magic_files/magic'))
Camillecamilo answered 21/2, 2013 at 16:26 Comment(1)
@picomon perfect questions. people with limited communication skills tend to forget mentioning such detailsPigsty
K
2

As the python-magic problems seems to be quite common, here a working solution fo future googlers: After testing most solutions without altering the source-code, I found the following to get python-magic working out of the box:

  1. Install GnuWin32 file first
  2. Set the environment variable MAGIC=path\to\gnuwin32\share\misc\magic
  3. Assure all installed executables/libraries to be accessible via the PATH
  4. Install python-magic via pip
Kaylenekayley answered 20/10, 2017 at 9:48 Comment(0)
R
2

Please try install this package:

pip install python-magic-bin
Raffin answered 15/4, 2021 at 15:55 Comment(0)
L
0

try this:

pip uninstall python-magic
pip uninstall python-magic-bin
pip install python-magic
pip install python-magic-bin

which means install python-magic-bin after python-magic

reference:https://github.com/ahupp/python-magic/issues/248

Leckie answered 11/3, 2022 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.