Skip to content Skip to sidebar Skip to footer

Embedding Python With Pybind11. Virtual Environment Doesn't Work

I am trying to make a simple c++ application which translates phrases using googletrans python library. So I've chosen pybind11 for this purpose to embed python. Also I use cmake f

Solution 1:

The solution involves two parts.

Compile the virtualenv PYTHONPATH into your C++ program
In CMake, this can be done with target_compile_definitions. The custom path will be available as a preprocessor macro.

target_compile_definitions(app PRIVATE -DCUSTOM_SYS_PATH="\"${CMAKE_HOME_DIRECTORY}/venv/Lib/site-packages\"")

Update the Python sys.path right starting the interpreter.

This is pybind-specific, but ought to look something like:

py::module sys = py::module::import("sys");
sys.attr("path").attr("insert")(1, CUSTOM_SYS_PATH);

Post a Comment for "Embedding Python With Pybind11. Virtual Environment Doesn't Work"