我想在pybind11中使用python虚拟环境。默认情况下,我的系统使用python2。我有一个使用python3的虚拟环境(使用虚拟包装器创建)。如何修改CMake以使用虚拟环境?virutalenv站点包的路径是
~.virtualenvs/name-of-virtual-env/lib/python3.6/site-packages我试过了
但这对我没有用。我用的是ubuntu。
这就是我所拥有的。
CmakeLists.txt
cmake_minimum_required(VERSION 3.4)
project(example)
add_subdirectory(pybind11)
add_executable(example src/main.cpp)
target_link_libraries(example PRIVATE pybind11::embed)main.cpp
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
using namespace py::literals;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::module_ sys = py::module_::import("sys");
py::print(sys.attr("path"));
py::print("python version: ", sys.attr("version_info"));
}输出
python version: sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)非常感谢你的帮助。
发布于 2021-10-11 14:24:48
最初的问题是,默认情况下,CMake FindPython脚本更喜欢系统Python而不是虚拟环境。您可以通过设置the Python_VIRTUALENV to FIRST (从CMake 3.15开始)来调整这一点。
set(Python_VIRTUALENV FIRST)要记住的另一点是,find_package调用将select the newest version of Python unless you specify an EXACT version。
https://stackoverflow.com/questions/68302655
复制相似问题