我正在使用 Cython 构建一个链接到 DLL 文件的 python 模块。为了成功导入我的模块,我需要在 Windows 搜索路径中包含 DLL。否则,典型的错误消息是:
ImportError:DLL 加载失败:找不到指定的模块。
有没有办法将DLL直接打包到生成的pyd文件中,方便分发?
这方面的一个例子是 OpenCV 分发,其中分发了一个(巨大的)pyd 文件,并且是 Python 绑定(bind)工作所需的唯一文件。
最佳答案
Python 的打包部署仍然是我们很多人的痛点。只是没有 Elixir 。以下是几种方法:
1。 OpenCV构建方法
OpenCV generates these wrapper functions automatically from the C++ headers using some Python scripts which are located in modules/python/src2.
基本上,它会解析头文件并在需要时生成 static PyObject
关键字。一旦正确创建了 header ,它就会调用 python setup
。老实说,它可能有用,但我不建议使用这种方法。
2。生成文件
如果您已经使用 Makefile,只需创建一个规则来放置您的库。例如,来 self 自己的代码:
setup.py
from distutils.core import setup, Extension
setup(name='sha1_hmac', version='1.0', \
ext_modules=[Extension('sha1_hmac',
library_dirs=['C:\MinGW\lib'],
sources= ['../tools/sha1.c','sha1_hmac.c'])])
Makefile
# The hmac generation used by the webserver is done
# using the sha1.c implementation. There is a binding needed to
# glue the C code with the python script
libsha1_hmac:
ifeq ($(OS), Windows_NT)
$(PYTHON) setup.py build --compiler=mingw32
else
$(PYTHON) setup.py install --home=$(CURDIR)
endif
.PHONY: webserver
webserver: libsha1_hmac
ifeq ($(OS), Windows_NT)
mv $(shell find build -type f -name "sha1*.pyd") $(LIB)
else
mv -f $(shell find $(LIB)/python -type f -name "sha1*.so") $(LIB)
endif
$(PYTHON) hmac_server.py
3。现代部署工具
有几种部署 Python 应用程序的新工具,即 wheels
,它们似乎越来越受欢迎。我不使用它,但它看起来可以缓解您的捆绑问题:
一旦启动
,您就可以像这样安装它:pip install some-package.whl
关于python - 如何将链接的 DLL 和 pyd 文件打包成一个独立的 pyd 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637251/