Create a module in Boost.Python with a structure equivalent to the Python module (maybe a package to be exact) shown below.
mainmodule/
├ __init__.py
├ submodule1/
│ ├ __init__.py
│ └ hoge.py
├ submodule2/
│ ├ __init__.py
│ └ foo.py
:
:
Import is
import mainmodule
from mainmodule.submodule1 import hogehoge # mainmodule/submodule1/hoge.hogehoge is defined in py
from mainmodule import submodule2
It will be like that.
This article describes a fairly complicated use of Boost.Python. Therefore, it is intended for people who have used Boost.Python at least once.
If you haven't used Boost.Python but are interested in it, click here letsboost :: python --Kmonos.net Please prepare.
I often find a way to nest classes, but I couldn't find nesting modules, so make a note. The original page Packages in Python extension modules
I think it's faster to see the source code, so it's the source code suddenly.
Declare the outermost module.
BOOST_PYTHON_MODULE(mainmodule) {
using namespace boost::python;
object package = scope();
package.attr("__path__") = "mainmodule";
export_submodule1();
export_submodule2();
:
}
Function that declares a submodule
void export_submodule1() {
using namespace boost::python;
// from mainmodule.submodule1 Allows to be called in the form of import
object module(handle<>(bp::borrowed(PyImport_AddModule("mainmodule.submodule1"))));
//from mainmodule import submodule1 can be used
scope().attr("submodule1") = module;
//Set scope
scope scope1 = module;
//Define various necessary functions and classes
def("hogehoge", &hogehoge);
:
}
It can be used like a normal Python module, so there is no particular explanation. The import is as shown at the beginning.