Pybind11嵌入式模块在导入时不运行



我花了很多小时试图找到导入模块不工作的原因。

py_actor.cpp:

#include <pybind11/embed.h>
#include "py_actor.h"
namespace py = pybind11;
void Py_AddSpeed(double speed) 
{
py_speed = speed;
}
PYBIND11_EMBEDDED_MODULE(py_actor, m) {
m.def("Py_AddSpeed", &Py_AddSpeed, "A function that adds speed to all actors");
}

test.py(在编译的py_actor.cpython-38-x86_64-linux-gnu.so的同一文件夹中(:

from py_actor import Py_AddSpeed
speed = 16.0
print('UPDATING SPEEED!')
Py_AddSpeed(speed)

主要内容:

py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello, World! GZDoom now runs Python :)"); // use the Python API
py::module test = py::module::import("test");

py::打印有效吗?对";"更新速度";工作为什么?我不知道。我已经重新编译了这个项目一千次了,导入的模块没有;运行";。我甚至试过这个:

py::exec(R"(
import test
print('Speeedd')
print(test.speed)
)");

没有,没有导入错误,也没有结果。我做错了什么?

我通过发现了问题所在

py::module test = py::module::import("test");
py::print(test.attr("__file__"));

就在那时,我意识到与某个内部模块存在冲突,于是我将该模块重命名为"_测试";它奏效了。我希望这能在未来拯救一个人。

最新更新