2011-11-02 62 views
1

这可能是一个微不足道的,但我有点卡在这里。Boost :: Python:嵌入和加载Boost :: Python模块和转换器

我有以下设置:

  • entity.cpp/.hpp:包含我的实体类的定义和实现。
  • entity_wrap.cpp:我这是我编译成entity.so
  • entity_test.cpp升压Python包装的文件:一份测试文件

我想什么entity_test.cpp做的是以下几点:

Py_SetProgramName(argv[0]); 
Py_Initialize(); 

... 
Entity* entity = new Entity; 
globals["entity"] = entity; 

我现在得到以下例外:

TypeError: No to_python (by-value) converter found for C++ type: Entity 

这是显而易见的,因为我不加载我的类型的转换定义。我现在试图加载entity.soglobals["entity_module"] = import("entity");,但我遇到了这个例外:

ImportError: No module named entity 

我可以从一个Python外壳预期加载模块。

我现在的问题是:如何加载entity_wrap.cpp中定义的转换器?


解决方案

由于eudoxos说,我必须确保我要加载的模块是sys.path

globals["sys"] = import("sys"); 
exec("sys.path.append('/path/to/my/module')\n" 
    "import entity", globals); 

现在它就像一个魅力。显然只是使用Py_SetProgramName(argv[0]);是不够的。

回答

1

With boost::python::import;观看sys.path虽然让你的模块被发现,你可能要添加一个调用

PyRun_SimpleString("import sys; sys.path.append('.');") 

第一。那么,您可以通过PyRun_SimpleString进行导入,然后:-)

另一种选择:在python中编写entity_test本身。

+0

eudoxos:非常感谢,我几乎在同一时间得到了类似的解决方案。我会相应地更新我的问题。 – Constantinius