我开始使用pybind11库了。它真的是一个很好的库,有很好的文档,它对我来说是有效的,但只能在Python2.7中使用。我不能让它在Python3.5上工作,我不知道我做错了什么。
这是我的"hello world“程序:
#include <pybind11/pybind11.h>
namespace py = pybind11;
const char * world() { return "Hello world!"; }
int main(int argc, char ** argv) {
Py_Initialize();
py::module m("hello", "my hello world Python module with pybind11");
m.def("world", &world);
PyRun_SimpleString("import hello\nprint( hello.world() )");
Py_Finalize();
return 0;
}如果我编译并链接到2.7,我会得到预期的结果:
Hello world!但是,如果我将完全相同的代码链接到Python3.5,我会在加载模块时出错。
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'hello'有什么想法吗?
发布于 2016-08-16 06:49:48
你是如何编译这个库的,你使用的是什么系统?如果您按照文档中的基本示例(下面的命令)中的建议使用了python-config,那么您将使用您机器上的开发版本进行编译。
c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so即使你使用Python3.5,这也很可能是Python2.7。在我的例子中,我使用Anaconda安装了python,这使我获得了Python3.5,但系统Python仍然是2.7。
python-config给了你什么?列出的版本是Python 3.5吗?如果不是,您至少不能在没有修改的情况下使用此命令。您所能做的就是按照文档中所描述的那样使用cmake构建系统。帮我修好了。使用他们的setuptools系统,可能也可以工作。基本上,他们会找出你用来编译他们的库的东西,并使用这些标志。这确实意味着,在构建库本身时,您必须确保链接到Python3.5。
https://stackoverflow.com/questions/37654018
复制相似问题