4

我正在使用 SWIG 为 Python 编写 C++ 扩展。根据我对 Python 和 SWIG 的了解,每个模块都必须有自己的 .pyd 文件。例如,如果我有一个名为“mymodule”的模块,则应该有一个相应的“_mymodule.pyd”文件。

在我的特殊情况下,我希望只有一个“pyd”文件和多个链接到它的模块。

mypackage/
mypackage/__init__.py
mypackage/module1.py
mypackage/module2.py

我不想维护多个.pyd,所以我可以将我的接口(.i)文件都放在VS2010中的同一个项目下。

编辑:到目前为止,我能够使其工作的唯一方法是将我的“pyd”文件复制到两个新文件中:_module1.pyd 和 _module2.pyd。但我不喜欢这个解决方案,因为我需要不必要地复制 30 Mo 的 'pyd' 文件。我希望模块链接到一个“_package.pyd”文件。

最好的方法是什么?

4

2 回答 2

0

最后,最简单和“正确”的做法是创建多个项目,只调用大型单体项目的一些暴露部分。SWIG 将解析较小的项目以创建 Python 模块。奇迹般有效。

于 2012-03-23T14:26:36.153 回答
-1

我只是有同样的问题我的解决方案使用 python ctypes 模块

spclient_python.pyd 是一个带有两个 init 函数的 pydll,官方的一个 initspclient_python 作为 dll 名称,另一个模块名为 example,带有 initexample 函数。python中的每个模块在dll中都有对应的initXXXXXXX函数。

import ctypes
testlib = ctypes.PyDLL("spclient_python.pyd") #this will call initspclient_python 
testlib.initexample() # here I am manually calling initexample function inside spclient_python.pyd
import sys
print (sys.modules.keys()) # this will display all the modules imported...spclient_python and example will show
import spclient_python # this is for me to use the module in the code below
import example
test1=spclient_python.test1()
test2=example.test2()
于 2018-12-10T03:55:42.010 回答