当导入带有嵌入式Python的SWIG模块时,出现“找不到名为_<module>的模块”。

2024-10-01 07:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在嵌入式python3.5.2中使用SWIG。以下是作为Windows控制台应用程序构建的。初始化Python端SWIG模块失败“阿皮.py“当它试图导入C++侧SWIG模块”时,“ARPY”。My(可能不正确)理解C++侧的“ARARY”模块应该已经被SWIG模块init函数从主()调用,但这似乎不是这样。在

阿皮。我:

%module arpy

%{
#include "arpy.h"
%}

%include <windows.i>
int test();

arpy.h:每小时

^{pr2}$

swig -python -c++ arpy.i生成:
阿皮_包装.cxx
阿皮.py在

在主.cpp公司名称:

#include <Python.h>

extern "C" PyObject* PyInit__arpy();

int main()
{
    Py_Initialize();
    PyInit__arpy(); // is this the right call to "import" the module?
    PyRun_SimpleString("import arpy");

    return 0;
}

int test()
{ 
    return 1;
}

输出:

Traceback (most recent call last):
  File "C:\Personal\Aue\Python\arpy.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\3rdParty\lib\Python\Python-3.5.2\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named '_arpy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Personal\Aue\Python\arpy.py", line 21, in <module>
    _arpy = swig_import_helper()
  File "C:\Personal\Aue\Python\arpy.py", line 20, in swig_import_helper
    return importlib.import_module('_arpy')
  File "C:\3rdParty\lib\Python\Python-3.5.2\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_arpy'

Python正在运行阿皮.py使用失败的初始化代码importlib.import_模块在“阿皮”上。在main()中调用PyInit_uarpy()我认为应该是通过CPython/capi“导入”SWIG生成的arpy模块,但显然这一切都不符合我的猜测。在


Tags: 模块inpyimportreturninitlineimportlib
1条回答
网友
1楼 · 发布于 2024-10-01 07:42:20

从这里的例子:(https://docs.python.org/3/extending/embedding.html) 我看到了将SWIG C++模块作为需要的内置程序导入:

PyImport_AppendInittab(“_arpy”,&PyInit_uuarpy)

调用Py()之前初始化

现在一切如期而至。在

相关问题 更多 >