Module python-magic on/for Windows?

Larry Hale larzluv at hotmail.com
Mon May 12 02:34:08 EDT 2008


On May 11, 11:42 pm, Larry Hale <larz... at hotmail.com> wrote:
> THANKS, AGAIN, for the reply!  :)
>
> Okay, now I -really- feel silly...  :>
>
> So, when I was going to try what you'd suggested, I noticed something
> peculiar: I hadn't "changed" anything on my system, but now, when I
> tried to "import magic" I got a missing DLL error.  For a DIFFERENT
> DLL!  This got me to thinking...
>
> Turns out, when I was having "success" earlier, I was -in- the C:
> \Program Files\GnuWin32\bin directory (where file.exe and related .dll
> files are).
>
> So I added C:\Program Files\GnuWin32\bin to the (system/Windows)
> PATH.  Everything worked.  I removed all the copious copies of the
> DLL, the magic files, etc., littering my hard drive in every other
> location.  Still worked.
>
> 'Course, I don't honestly know why it didn't work with the files
> copied to other locales contained in the sys/Py paths... perhaps it
> was, as you mentioned, wanting the tree-structure to be the same?  (I
> haven't the heart to check this out fully right now... *maybe* at a
> later time...  ;) )
>
> Yeah, I know.  But HONESTLY!  I could have -sworn- I'd already tried
> THAT... you know, all the "obvious"/"I've been programming/working in
> computers (albeit 'Windows' until lately) for ~25 years; yeah, I'm a
> 'hacker'..." stuff.  I -swear-!
>
> [EGG ON FACE]
>
> [SIGH]  Well, I'd say "live and learn", but I'm still scratching my
> head.  But, 'nough of that.  Thanks for your replies, Michael, and I'm
> off to work _with_ the module now, then work-up my HowTo (and
> experiment with making an "egg"?? ;) )...
>
> Caio!
> -Larry  :P  :)
>
> On May 11, 11:09 pm, Michael Torrie <torr... at gmail.com> wrote:
>
> > Larry Hale wrote:
> > > ALSO: I've even tried putting the 4 "magic" files INTO the .egg
> > > file... still no-go.  :/
>
> > It's often the custom of programs ported from unix to windows to use the
> > dll location as a key to find the other files that are typically in
> > share, or etc.  GTK, for example uses ../share and ../etc from the
> > location where the dlls are stored (if the dlls are in a folder called bin).
>
> > In this case I'd try making a folder in the Python25 folder called
> > "share" and put the contents of the gnuwin32 share/file stuff in there.
> >  Should look something like this:
>
> > c:/python25/share/file/magic.mime.mgc
> > c:/python25/share/file/magic
> > c:/python25/share/file/magic.mgc
> > c:/python25/share/file/magic.mime
>
> > Or, judging by a string I found in the dll itself, you might have to put
> > the files here:
>
> > c:/progra~1/File/share/file/magic
>
> > Although if such a path really is hardcoded into the dll, this is
> > certainly a bug, since it will fail on certain non-english windows systems.
>
> > Anyway, who knows.  Give these options a try.



UPDATE: HOORAY!!!  Well, actually, was about two steps forward, one
back.  But I've gotten it all sorted out now!  :)

I added C:\Program Files\GnuWin32\bin to the system/Windows PATH.
This takes care of the various required DLLs/libs.

The file source (previously linked from http://hupp.org/adam/hg/python-magic/)
has the man pages.  I'd overlooked these as they were only modified as
necessary; the "BSD" part made me think they'd be the same as the same
online versions I'd already read (that were *nix specific).
Obviously, I was wrong.  These had the default paths to the magic*
files listed; C:\Program Files\File\share\file\.

If one creates said directory structure and moves/copies/unpacks the
magic ("database") files THERE, then one may do:

    >>> import magic
    >>> test = magic.Magic() # <-- New Magic class instance
    >>> test.from_file( 'C:\\startrek.exe' ) # <-- Yeah, I have an old
Star Trek game file here to test with...  :)
    'MS-DOS executable, MZ for MS-DOS'  # <-- This is what's
returned...

Alternately, if one wishes to leave/place the magic files elsewhere,
do like:

    >>> test = magic.Magic( magic_file = 'C:\\Program Files\\GnuWin32\
\share\\file\\magic' ) # <-- spec where/what the file is

NOTE: Even if the "magic_file" location *is* specified, "mime = True"
still works fine.  (Haven't checked, but I presume the source simply
tacks ".mime" to the filename automagically.)  Obviously/also, one
needn't specify the argument names if one uses proper argument order:
magic.Magic( mime, magic_file )  :)


THANKS SO MUCH, Michael, for your answers and helping me alone the
way...  :)


Cheers,
-Larry



More information about the Python-list mailing list