Embedding pybind11 with virtual environment

跟風遠走 提交于 2019-12-13 16:25:03

问题


I'm trying to embed python using pybind11 into my C++ application. Using the following CMake property, I managed to compile against a virtual environment of my project.

-DPYTHON_EXECUTABLE:FILEPATH=C:/Python/Envs/myproject/Scripts/python.exe

When I run the application I get an error (below) without a specific error. However I assume it fails to load the module numpy which I'm loading.

abort() has been called

#include <iostream>
#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{};
    auto sys = py::module::import("sys");
    py::print("Hello, World from Python!");
    py::print(sys.attr("executable"));
    py::print(sys.attr("version"));
    // works until here

    auto np = py::module::import("numpy");
    py::print(np.attr("version"));

    return EXIT_SUCCESS;
}

If I only import the sys module (which is in the standard library) the application works fine. This is the output of the application until the crash:

Hello, World from Python!
C:\Develop\sandbox\python_binding\cmake-build-debug\bin\python_binding.exe
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]

I've added the following directories to my PATH.

PATH=C:\Python\Envs\project\Scripts;C:\Python\Envs\project\Lib

Do I have to specify the paths to other parts of the python interpreter I'm using or could this be caused by another problem?


回答1:


I had to set the PYTHONPATH to site-packages

set PYTHONPATH=C:\Python\Envs\project\Lib\site-packages

instead of C:/Python/Envs/project/



来源:https://stackoverflow.com/questions/53480120/embedding-pybind11-with-virtual-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!