如何将链接的DLL和pyd文件打包到一个自包含的pyd文件中?

toi*_*ine 16 python windows dll cython

我正在使用Cython构建一个链接DLL文件的python模块.为了成功导入我的模块,我需要在Windows搜索路径中使用DLL.否则,典型的错误消息是:

ImportError: DLL load failed: The specified module could not be found.

有没有办法将DLL直接打包到生成的pyd文件中以使分发更容易?

这方面的一个例子是OpenCV发行版,其中分发了(巨大的)pyd文件,并且是Python绑定工作所需的唯一文件.

luc*_*asg 9

Python的打包和部署仍然是我们许多人的痛点.没有银弹.以下是几种方法:


1. OpenCV构建方法

该方法在此处描述:https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.html#bindings-basics

OpenCV使用一些位于modules/python/src2中的Python脚本从C++头文件自动生成这些包装函数.

基本上它会解析头文件并在static PyObject需要时生成关键字.一旦标题被适当地创建,它就会调用python setup.老实说,它可能会奏效,但我不会建议这种方法.

2. Makefiles

如果您已经使用Makefile,只需创建一个规则来放置您的lib accordinlgy.例如,来自我自己的代码:

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'])])
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

3.现代部署工具

部署python应用程序有几种新工具,这些工具wheels似乎很有吸引力.我不使用它,但看起来它可以缓解你的捆绑问题:

一旦它wheeled,您可以像这样安装它:pip install some-package.whl