Boost::Python:嵌入并加载Boost::Python模块和转换器
Boost::Python: embed and load Boost::Python modules and converters
这可能是一个微不足道的问题,但我有点拘泥于此。
我有以下设置:
entity.cpp/.hpp
:包含我的Entity类定义和实现entity_wrap.cpp
:我的boost python包装文件,我将其编译为entity.so
entity_test.cpp
:测试文件
我想在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
这是显而易见的,因为我没有加载类型的转换定义。我现在尝试用globals["entity_module"] = import("entity");
加载entity.so
,但遇到了这个异常:
ImportError: No module named entity
我可以按预期从python shell加载模块。
我现在的问题是:如何加载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]);
是不够的。
使用boost::python::import
;注意sys.path
,以便找到您的模块,您可能需要添加对的调用
PyRun_SimpleString("import sys; sys.path.append('.');")
首先。好吧,你也可以通过PyRun_SimpleString
进行导入,然后:-(
另一种选择是:用python编写entity_test
本身。
相关文章:
- 我应该包含什么来制作 boost.python 扩展?
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- Boost Python Numpy - 要初始化的未定义引用
- Boost.Python :C++模板类型匹配的嵌套命名空间
- Boost.Python 列出了所有公开的类和属性
- Boost.Python 在静态库方面失败
- 在cpp中使用boost-python的python代码是否进行动态内存分配
- Boost.Python 和导入 dll,"The specified module could not be found"
- 无法将__str__特殊方法与Boost::Python接口
- 编译时出现Boost.python链接错误
- 使用 Boost/Python 的未定义符号 - 复杂
- Boost.Python 和C++导入到 Python 3 时编译库错误
- 两个并发的 Python 进程可以在 Boost Python 中运行吗?
- 通过 Boost Python 将 Python 函数转换为 C++,用作回调
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- Boost.Python.ArgumentError: World.set(World, str) 中的 Python
- 使用 boost python 从 c++ 为 python 中的类成员变量赋值
- Boost Python 不断链接 Python27 库
- 使用 python3 为 msvc 构建 boost python - 链接器错误