问题
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