“No module named '_'” when importi

2019-07-30 00:42发布

问题:

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 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;
}

Output:

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 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.

回答1:

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);

before calling Py_Initialize()

Things work as expected now.