raincloud raincloud - 1 year ago 259
Python Question

Python-magic has OSError: [WinError 193] error while running in 32-bit version of IDLE

I have been trying to install the module python-magic for a few hours, and I've been encountering some problems. I am using the 32-bit version of Python 3.5.2 with 64-bit Windows 7.

First, I used the command "

pip install python-magic
". I downloaded magic1.dll, regex2.dll, and zlib1.dll, and magic from the Files for Windows project, and I copied those four files to

C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info.

I added the aforementioned directory to the beginning of PATH in my Windows environment variables. Then I opened IDLE's Shell and typed "
import magic
" and got the response
OSError: [WinError 126] The specified module could not be found

I read that the "magic" file should have the extension .dll, so I renamed it. This led to the pop-up warning by Windows
"C:\Program Files (x86)\Python35-32\Lib\site-packages\magic.dll is either not designed to run on Windows or contains an error"
and by Python,
"OSError: [WinError 193] %1 is not a valid Win32 application
". I read that the latter error is commonly encountered while running it in a 64-bit environment, but I made sure to run it in 32-bit IDLE and only have the 32-bit version of Python installed.

Following the advice of previous StackOverflow posts, I tried copying cygmagic-1.dll, cygwin1.dll, and cygz.dll to C:\Windows\System32, to the same folder as magic.dll, and I also tried renaming cygmagic-1.dll as magic1.dll, but that didn't have any effect. I know other places say you're not supposed to mix Cygwin Python and Windows Python, but I tried it without the involvement of these files, and it didn't work then, either.

I tried renaming magic.dll to magic.exe, and that allowed "import magic" and magic.
Magic(magic_file=r'C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info\magic.exe') with the response "<magic.Magic object at 0x02EA0A70>". When I tried testing with magic.from_file(r'C:\Program Files (x86)\Python35-32\Lib\site-packages\README.txt'), though, I got the error magic.MagicException: b'could not find any magic files!
I figured that renaming it to magic.exe had to be wrong, but that it was worth a try.

After I gave up on python-magic, someone recommended an older project. I downloaded it and put the pymagic folder in my site-packages directory. When I tried to import pymagic.pymagic, it told me that the module StringIO doesn't exist, and the recommender told me it was because StringIO is from Python2. I changed all mentions of StringIO to io and tried the command

This generated the error
TypeError: startswith first arg must be bytes or a tuple of bytes, not str.
I'm not involved enough with Python's os, io, etc. modules to know how to make modifications to get this to work. Can anyone make any recommendations on how to get python-magic or pymagic working, or any other module for identifying a file based on its header? I know this question has been asked a lot, but the previous answers didn't work out for me.

Answer Source

Did you call the 'magic' data file magic, and leave it in the same folder as magic1.dll?

Following your instructions I was able to reproduce the same error as you. Using Sysinternals Process Monitor, I could see that the reason for your first error appeared to be that Python was trying to load the the magic data file as if it were the library.

I then renamed the magic data file to magic_data, restarted IDLE, and it worked. I could then use magic to identify a file:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import magic
>>> fn = r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\magic_data'
>>> m = magic.Magic(magic_file=fn)
>>> m.from_file(r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\zlib1.dll')
'PE32 executable for MS Windows (DLL) (console) Intel 80386 32-bit'

(I'm using a different version of Python (3.4), and a different version of Windows (10) to you, but I don't think these matter too much.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download