在Windows 64位上导入魔术的问题

时间:2017-06-11 11:50:52

标签: python python-2.7 import python-magic

我在互联网上搜索了在Windows 10 64位中正确导入magic的解决方案。我找到this,但我不明白解决方案。

  1. 它表示将两个DLL复制到OS中的指定路径中。

  2. 以下命令是什么意思?

    file_magic = magic.Magic(magic_file="c:\path\to\magic.mgc")
    
  3. 解决方案是1 + 2的组合吗?还是只有1?我做了1.仍然出错。 (第1个错误已解决)

    >>> import magic
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\magic.py", line 176, in <modu
      raise ImportError('failed to find libmagic.  Check your install
    ImportError: failed to find libmagic.  Check your installation
    

    我得到的第二个错误

    >>> import magic
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\site-packages\magic\__init__.py", line 1, in <module>
        from . import ffi
      File "C:\Python27\lib\site-packages\magic\ffi.py", line 27, in <module>
        ext_package="magic")
      File "C:\Python27\lib\site-packages\cffi\api.py", line 437, in verify
        lib = self.verifier.load_library()
      File "C:\Python27\lib\site-packages\cffi\verifier.py", line 113, in        load_library
        self._compile_module()
      File "C:\Python27\lib\site-packages\cffi\verifier.py", line 210, in _compile_module
        outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
      File "C:\Python27\lib\site-packages\cffi\ffiplatform.py", line 29, in compile
        outputfilename = _build(tmpdir, ext, compiler_verbose)
      File "C:\Python27\lib\site-packages\cffi\ffiplatform.py", line 55, in _build
        dist.run_command('build_ext')
      File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
      File "C:\Python27\lib\site-packages\setuptools\command\build_ext.py", line 75, in run
    _build_ext.run(self)
      File "C:\Python27\lib\distutils\command\build_ext.py", line 340, in run
    self.build_extensions()
      File "C:\Python27\lib\distutils\command\build_ext.py", line 449, in build_extensions
    self.build_extension(ext)
      File "C:\Python27\lib\site-packages\setuptools\command\build_ext.py", line 196, in build_extension
    _build_ext.build_extension(self, ext)
      File "C:\Python27\lib\distutils\command\build_ext.py", line 499, in build_extension
    depends=ext.depends)
      File "C:\Python27\lib\distutils\msvc9compiler.py", line 473, in compile
    self.initialize()
      File "C:\Python27\lib\distutils\msvc9compiler.py", line 383, in initialize
    vc_env = query_vcvarsall(VERSION, plat_spec)
      File "C:\Python27\lib\site-packages\setuptools\msvc.py", line 136, in msvc9_query_vcvarsall
    return EnvironmentInfo(arch, ver).return_env()
      File "C:\Python27\lib\site-packages\setuptools\msvc.py", line 1097, in return_env
    [self.VCIncludes,
      File "C:\Python27\lib\site-packages\setuptools\msvc.py", line 805, in VCIncludes
    return [os.path.join(self.si.VCInstallDir, 'Include'),
      File "C:\Python27\lib\site-packages\setuptools\msvc.py", line 542, in VCInstallDir
        raise distutils.errors.DistutilsPlatformError(msg)
     distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 9.0 is required.     Get it from http://aka.ms/vcpython27
    

1 个答案:

答案 0 :(得分:0)

我认为您已成功完成第1步。第2步将您的magic.mgc文件(通过克隆libmagicwin64获得)保存在python脚本可访问的地方(在下面的案例中< strong> C:\ Programs \ libmagicwin64 \ 文件夹)。然后就可以这样使用......

>>> import magic
>>> file_magic = magic.Magic(magic_file="C:\Programs\libmagicwin64\magic.mgc")
>>> print(file_magic.from_file("D:\Work Folder\mcabcaaug2016.pdf"))
'PDF document, version 1.4'