在 Windows 上的 python 中使用 libarchive [英] Using libarchive in python on Windows
问题描述
我正在尝试让 libarchive 模块在 Windows 上的 python 3.4 中工作.我已经用 pip 安装了 libarchive-c 并且一切正常,但是每当我尝试将它导入我的代码甚至单独运行它时,我都会收到错误:
I'm trying to get libarchive module working in python 3.4 on windows. I've installed libarchive-c with pip and all went ok but whenever I try to import it into my code or even to run it alone I'me getting error:
OSError: [WinError 126] The specified module could not be found
这来自以下代码的 ffi.py:
This is coming from ffi.py from the code below:
libarchive_path = os.environ.get('LIBARCHIVE') or find_library('archive')
libarchive = ctypes.cdll.LoadLibrary(libarchive_path)
我以前从未使用过 ctypes,但如果我理解正确,它正在寻找外部 DLL.于是找到并安装了http://gnuwin32.sourceforge.net/packages/libarchive.htm我还在环境变量中将 C:\Program Files (x86)\GnuWin32\bin 添加到我的 %PATH% 中,但它仍然无法加载模块.因为它没有给我名字,所以我不确定它在寻找什么模块.我错过了什么?
I've never used ctypes before but if I understand correctly it is looking for external DLL. So found and installed http://gnuwin32.sourceforge.net/packages/libarchive.htm also I've added C:\Program Files (x86)\GnuWin32\bin to my %PATH% in environmental variables but it still cannot load the module. As it does not give me the name I'm not sure what module it is looking for. What am I missing?
推荐答案
(免责声明)我贡献于 https://github.com/Changaco/python-libarchive-c 和我维护 https://github.com/nexB/scancode-toolkit
(disclaimer) I contribute to https://github.com/Changaco/python-libarchive-c and I maintain https://github.com/nexB/scancode-toolkit
两者都包含 libarchive
的 ctypes
绑定,尽管 ScanCode 仅用于提取.
Both contain a ctypes
binding for libarchive
, though ScanCode is for extraction only.
我在这里的回答是针对 python-libarchive-c
,但 ScanCode 包含您正在寻找的一些 DLL,所以我将两者结合起来.
My answer here is for python-libarchive-c
, but ScanCode contains some of the DLL you are looking for so I am lacing in a bit of both.
要在 Windows 上运行 python-libarchive-c
,您需要一个 libarchive
DLL 及其可以加载的 deps.
To get python-libarchive-c
going on Windows you need a libarchive
DLL and its deps that can then be loaded.
python-libarchive-c
中没有捆绑预构建的 DLL,但我在这里为另一个项目预构建了 Windows 二进制文件:https://github.com/nexB/scancode-toolkit/tree/develop/src/extractcode/bin/win-32/bin对应的源代码在那里:https://github.com/nexB/scancode-thirdparty-src如果你想自己从源代码重建,你有 MinGW32
构建说明:https://github.com/nexB/scancode-thirdparty-src/blob/master/libarchive/build.sh#L47
There are no pre-built DLLs bundled in python-libarchive-c
but I have prebuilt Windows binaries for another project here:
https://github.com/nexB/scancode-toolkit/tree/develop/src/extractcode/bin/win-32/bin
The corresponding source code is there: https://github.com/nexB/scancode-thirdparty-src
And you have MinGW32
build instructions there if you want to rebuild from source yourself: https://github.com/nexB/scancode-thirdparty-src/blob/master/libarchive/build.sh#L47
通常从路径加载 DLL -- 假设 var libarchive
包含该 DLL 的完整路径 -- 使用以下命令:<代码>lib = ctypes.CDLL(libarchive)现在这是扫描码.对于 python-libarchive-c
,您可以尝试将 LIBARCHIVE
变量设置为指向 DLL 的路径:set LIBARCHIVE="C:\.....\libarchive.dll"
In general to load a DLL from a path -- assuming that the var libarchive
contains the full path to that DLL -- use this:
lib = ctypes.CDLL(libarchive)
Now this is for Scancode. For python-libarchive-c
, you could try to set the LIBARCHIVE
variable to point the path of your DLL with:
set LIBARCHIVE="C:\.....\libarchive.dll"
然后启动Python,导入库并使用.
Then start Python, import the library and use it.
注意:我(还)没有对此进行测试,但这应该可行.如果不是,请提交错误.我也没有在 Python 3.4 上运行任何测试.我主要使用 Python 2.7.但是 DLL 和代码根本不是 Python 2.7 特定的.
NB: I did not test this (yet) , but this should work. IF not please file a bug. I did not run any test on Python 3.4 either. I use primarily Python 2.7. But the DLL and the code is not Python 2.7-specific at all.
FWIW,扫描码加载库的方式有点复杂,因为它可以从相同的代码加载 DLL,用于使用传统位置的特定 32 或 64 位架构.您可以在那里看到正在运行的代码:https://github.com/nexB/scancode-toolkit/blob/develop/src/extractcode/libarchive2.py#L64
FWIW, the way scancode loads the library is a tad more engaged since it can from the same code load DLLs Win/Linux/Mac for specific 32 or 64 bits archs using conventional locations. You can see the code in action there: https://github.com/nexB/scancode-toolkit/blob/develop/src/extractcode/libarchive2.py#L64
ScanCode 还没有使用 python-libarchive-c
ATM,而是一个不同的/自定义的 ctypes 绑定,只专注于更具体的提取用例.至少它可以让您访问 Win DLL 及其依赖项(或构建它们的指令)以及有关如何正确加载它的示例.
ScanCode is NOT using python-libarchive-c
ATM yet but a different/custom ctypes binding focused on a more specific use case of extraction only. At least it gives you access to a Win DLL and its deps (or instruction a build them) and an example on how to load it correctly.
/HTH
这篇关于在 Windows 上的 python 中使用 libarchive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!