如何在 Clion 中使用虚拟环境从 C++ 调用 Python?

How to use a virtual environment to call Python from C++ in Clion?

我想使用虚拟环境在 Clion 中从 C++ 调用 Python。

当我在终端上 运行 时,我可以在 C++ 上使用 Py_Initialize();,但是当我在 Clion 上 运行 时,我无法使用它。
以下是在Clion中运行ning时的报错信息

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fbd24916680 (most recent call first):

在终端上,我使用 GNU Make 构建并通过进入 Anaconda 虚拟环境 运行 代码。然后代码按预期工作。

另一方面,在 Clion 上,我认为如果我从 File/Build, Execution, Deployment/Python Interpreter/ 开始在 Python Interpreter 中设置 Anaconda 虚拟环境,它会在 运行 时被引用。但是,好像引用没有正常工作,出现了上面的错误。我在没有进入虚拟环境的情况下运行终端里的代码也出现了上面的错误

有人知道如何设置吗?
谢谢。

== 附加部分==
这是CMakeList.txt

cmake_minimum_required( VERSION 3.6 )
# Create Project
project( Python2CPP )

# Require C++14 (or Later)
set( CMAKE_CXX_STANDARD 14)
set( CMAKE_CXX_STANDARD_REQUIRED ON )

find_package(OpenCV REQUIRED)

add_definitions("-DNOMINMAX")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

add_executable( 
    Python2CPP
    src/main.cpp
    src/Python2CPP.cpp
    src/Python2CPP.h )

set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "Python2CPP" )

# virtual python env from anaconda
set(PYTHON_INCLUDE_DIRS /home/masahiro/anaconda3/envs/virt/include/python3.6m)
set(NUMPY_INCLUDE_DIRS /home/masahiro/anaconda3/envs/virt/lib/python3.6/site-packages/numpy/core/include)
set(PYTHON_LIBRARIES /home/masahiro/anaconda3/envs/virt/lib/libpython3.6m.so)

include_directories(
    ${PYTHON_INCLUDE_DIRS}
    ${NUMPY_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS})

target_link_libraries(
    Python2CPP
    ${PYTHON_LIBRARIES}
    ${OpenCV_LIBS}
)

我已经修改了您的 CMakeLists.txt 以使用查找 Python 解释器和 Python 库的标准方法。你能放手吗?如果您有 CMake 3.14 或更高版本,那么找到 NumPy 的能力已经 built in.

cmake_minimum_required( VERSION 3.14 )
# Create Project
project( Python2CPP )

# Require C++14 (or Later)
set( CMAKE_CXX_STANDARD 14)
set( CMAKE_CXX_STANDARD_REQUIRED ON )

find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED)
find_package(OpenCV REQUIRED)

add_definitions("-DNOMINMAX")

add_executable(
    Python2CPP
    src/main.cpp
    src/Python2CPP.cpp
    src/Python2CPP.h)

set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "Python2CPP" )

target_include_directories(Python2CPP PRIVATE ${Python3_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${Python3_NumPy_INCLUDE_DIRS})

target_link_libraries(Python2CPP PRIVATE ${Python3_LIBRARIES} ${OpenCV_LIBS})

否则,如果您有旧版本的 CMake,那么您可以添加一个测试来检查 NumPy 是否可用:

cmake_minimum_required( VERSION 3.6 )
# Create Project
project( Python2CPP )

# Require C++14 (or Later)
set( CMAKE_CXX_STANDARD 14)
set( CMAKE_CXX_STANDARD_REQUIRED ON )

find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)

find_package(OpenCV REQUIRED)

# Find NumPy location
execute_process(
        COMMAND
        ${PYTHON_EXECUTABLE} "-c" "import re, numpy; print(re.compile('/__init__.py.*').sub('',numpy.__file__))"
        RESULT_VARIABLE _numpy_status
        OUTPUT_VARIABLE _numpy_location
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT _numpy_status)
    set(NumPy ${_numpy_location} CACHE STRING "Location of NumPy")
endif()

# Find NumPy version
execute_process(
        COMMAND
        ${PYTHON_EXECUTABLE} "-c" "import numpy; print(numpy.__version__)"
        OUTPUT_VARIABLE _numpy_version
        ERROR_QUIET
        OUTPUT_STRIP_TRAILING_WHITESPACE
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(NumPy
        FOUND_VAR NumPy_FOUND
        REQUIRED_VARS NumPy
        VERSION_VAR _numpy_version)

add_definitions("-DNOMINMAX")

add_executable( 
    Python2CPP
    src/main.cpp
    src/Python2CPP.cpp
    src/Python2CPP.h )

set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "Python2CPP" )

target_include_directories(Python2CPP PRIVATE ${PYTHON_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})

target_link_libraries(Python2CPP PRIVATE ${PYTHON_LIBRARIES} ${OpenCV_LIBS})

Python虚拟环境

在构建 C++ 程序之前,激活您的 Python 虚拟环境。如果您使用的是 CLion,它有终端选项卡。 Select 然后像这样激活您的虚拟环境:source <virtual environment path>/bin/activate。然后 cmake 应该找到正确的 Python 解释器。