"No module named '_<module>'" when importing a SWIG module with embedded Python
<p>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.</p>

<p>arpy.i:</p>

<pre><code>%module arpy

%{
#include "arpy.h"
%}

%include &lt;windows.i&gt;
int test();
</code></pre>

<p>arpy.h:</p>

<pre><code>#pragma once

int test();
</code></pre>

<p><code>swig -python -c++ arpy.i</code> generates:<br>
arpy_wrap.cxx<br>
arpy.py</p>

<p>main.cpp:</p>

<pre><code>#include &lt;Python.h&gt;

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;
}
</code></pre>

<p>Output:</p>

<pre><code>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 "&lt;frozen importlib._bootstrap&gt;", line 986, in _gcd_import
  File "&lt;frozen importlib._bootstrap&gt;", line 969, in _find_and_load
  File "&lt;frozen importlib._bootstrap&gt;", 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 "&lt;string&gt;", line 1, in &lt;module&gt;
  File "C:\Personal\Aue\Python\arpy.py", line 21, in &lt;module&gt;
    _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'
</code></pre>

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