使用嵌入式Python导入SWIG模块时

使用嵌入式Python导入SWIG模块时

本文介绍了“没有名为'_< module>'的模块"使用嵌入式Python导入SWIG模块时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SWIG与嵌入式Python 3.5.2一起使用.以下是作为Windows控制台应用程序构建的.尝试导入C ++侧SWIG模块"_arpy"时,它无法初始化Python侧SWIG模块"arpy.py".我(可能是错误的)理解到C ++端的"_arpy"模块应该已经由main()调用的SWIG模块init函数已经加载了,但事实并非如此.

I'm trying to use SWIG with embedded Python 3.5.2. The following is built as a Windows console app. It fails initializing the Python side SWIG module "arpy.py" when it tries to import the C++ side SWIG module "_arpy". My (probably incorrect) understanding it that the C++ side "_arpy" module should already be loaded by the SWIG module init function called from main() but this doesn't seem to be the case.

arpy.i:

%module arpy

%{
#include "arpy.h"
%}

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

arpy.h:

#pragma once

int test();

swig -python -c++ arpy.i生成:
arpy_wrap.cxx
arpy.py

swig -python -c++ arpy.i generates:
arpy_wrap.cxx
arpy.py

main.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正在运行arpy.py初始化代码,该代码在"_arpy"上使用importlib.import_module失败.我认为应在main()中调用PyInit__arpy(),应该通过CPython/C API导入" SWIG生成的_arpy模块,但显然这对我的猜测无效.

Python is running the arpy.py init code which fails using importlib.import_module on "_arpy". Calling PyInit__arpy() in main() I think should be "importing" the SWIG generated _arpy module via the CPython/C API but apparently this all doesn't work how I'm guessing.

推荐答案

从此处的示例中:( https://docs.python.org/3/extending/embedding.html )我看到要将SWIG C ++模块作为内置导入,您需要:

From the examples here: (https://docs.python.org/3/extending/embedding.html)I see that to import the SWIG C++ module as a builtin you need:

PyImport_AppendInittab("_ arpy",& PyInit__arpy);

PyImport_AppendInittab("_arpy", &PyInit__arpy);

在调用Py_Initialize()之前

before calling Py_Initialize()

现在一切正常.

这篇关于“没有名为'_&lt; module&gt;'的模块"使用嵌入式Python导入SWIG模块时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:15