【发布时间】:2017-04-03 18:39:51
【问题描述】:
我已经尝试安装模块python-magic 几个小时了,但遇到了一些问题。我正在使用 32 位版本的 Python 3.5.2 和 64 位 Windows 7。
首先,我使用了命令“pip install python-magic”。我从 Files for Windows 项目中下载了 magic1.dll、regex2.dll 和 zlib1.dll 和 magic,并将这四个文件复制到了
C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info.
我在我的 Windows 环境变量中将上述目录添加到 PATH 的开头。然后我打开IDLE的Shell,输入“import magic”,得到了回复OSError: [WinError 126] The specified module could not be found。
我读到“魔法”文件应该有扩展名 .dll,所以我重命名了它。这导致 Windows "C:\Program Files (x86)\Python35-32\Lib\site-packages\magic.dll is either not designed to run on Windows or contains an error" 和 Python "OSError: [WinError 193] %1 is not a valid Win32 application 弹出警告。我读到后一个错误在 64 位环境中运行时经常遇到,但我确保在 32 中运行它-bit IDLE 并且只安装了 32 位版本的 Python。
按照previous StackOverflow posts 的建议,我尝试将cygmagic-1.dll、cygwin1.dll 和cygz.dll 复制到C:\Windows\System32 和magic.dll 相同的文件夹中,我还尝试重命名cygmagic-1.dll 作为magic1.dll,但这没有任何效果。我知道其他地方说你不应该混合使用 Cygwin Python 和 Windows Python,但我在没有这些文件参与的情况下尝试了它,然后它也不起作用。
我尝试将magic.dll重命名为magic.exe,并且允许“import magic”和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!我认为将其重命名为magic.exe一定是错误的,但值得一试。
在我放弃 python-magic 之后,有人推荐了一个older project。我下载了它并将 pymagic 文件夹放在我的站点包目录中。当我尝试导入 pymagic.pymagic 时,它告诉我模块 StringIO 不存在,推荐者告诉我这是因为 StringIO 来自 Python2。我将所有提到的 StringIO 都更改为 io 并尝试了命令pymagic.pymagic.identify_file(r'E:\Pictures\picture.jpg')
这产生了错误TypeError: startswith first arg must be bytes or a tuple of bytes, not str. 我对 Python 的 os、io 等模块的参与不够多,不知道如何进行修改以使其正常工作。任何人都可以就如何让 python-magic 或 pymagic 工作,或任何其他基于文件头识别文件的模块提出任何建议吗?我知道这个问题被问了很多,但以前的答案对我没有用。
【问题讨论】:
标签: python python-magic