如何在 Ubuntu 20.04 上从 C++ 调用 Python 函数
How to call a Python function from C++ on Ubuntu 20.04
我目前在 class 中,我被要求在 Python 和 C++ 之间建立集成,因为我们稍后将在 class 中使用两者。我的讲师提供了一些简单的 C++ 和 Python 代码,因此我们可以测试集成,并且还提供了有关如何使用 Visual Studio 进行设置的说明。我目前只能访问 Ubuntu,我的(非常旧的)笔记本电脑将无法处理 VM。
我目前使用 Clion 作为我的 IDE,使用 Anaconda 作为虚拟环境。我花了一天的大部分时间试图解决这个问题,我相信这个问题与我的 CMake 文件有关,因为我现在对 CMake 几乎一无所知,但计划尽快学习它。
CMakeLists.txt、main.cpp、myPython.py都在主工程目录下
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(myProject main.cpp)
target_include_directories(myProject PUBLIC /usr/include/python3.9)
target_link_directories(myProject PUBLIC /usr/lib/python3.9)
target_link_libraries(myProject PUBLIC python3.9)
对于 CMakeLists.txt,我也尝试了以下方法,但无论我使用哪一个,都得到了相同的结果 error/output
cmake_minimum_required(VERSION 3.21)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
add_executable(myProject main.cpp)
target_link_libraries(myProject PUBLIC Python3::Python)
main.cpp
#include <python3.9/Python.h>
#include <iostream>
#include <string>
using namespace std;
// the main in the provided code is void but Clion gave me an error saying to change it to int
int main()
{
cout << "Start 1 \n";
Py_Initialize();
cout << "2\n";
PyObject* my_module = PyImport_ImportModule("myPython");
cerr << my_module << "\n";
PyErr_Print();
cout << "3\n";
PyObject* my_function = PyObject_GetAttrString(my_module,
"printsomething");
cout << "4\n";
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
return 0;
}
myPython.py
import re
import string
def printsomething():
print("Hello from Python!")
预期输出是
Start 1
2
01592AE0 // this will vary since it's the .py files location
3
4
Hello from Python!
我 运行 得到的是
Start 1
2
0 // output is red
3
ModuleNotFoundError: No module named 'myPython' // output is red
由于我不熟悉 CMake,并且在涉及 Ubuntu 时会认为自己是“普通用户”,因此我可能需要有关所提供的任何步骤的一些额外详细信息。
在 Py_Initialize();
之后的主函数中添加这个
PySys_SetPath(L".:/usr/lib/python3.8");
这是 python 模块的搜索路径。添加.
在当前路径搜索
我目前在 class 中,我被要求在 Python 和 C++ 之间建立集成,因为我们稍后将在 class 中使用两者。我的讲师提供了一些简单的 C++ 和 Python 代码,因此我们可以测试集成,并且还提供了有关如何使用 Visual Studio 进行设置的说明。我目前只能访问 Ubuntu,我的(非常旧的)笔记本电脑将无法处理 VM。
我目前使用 Clion 作为我的 IDE,使用 Anaconda 作为虚拟环境。我花了一天的大部分时间试图解决这个问题,我相信这个问题与我的 CMake 文件有关,因为我现在对 CMake 几乎一无所知,但计划尽快学习它。
CMakeLists.txt、main.cpp、myPython.py都在主工程目录下
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(myProject main.cpp)
target_include_directories(myProject PUBLIC /usr/include/python3.9)
target_link_directories(myProject PUBLIC /usr/lib/python3.9)
target_link_libraries(myProject PUBLIC python3.9)
对于 CMakeLists.txt,我也尝试了以下方法,但无论我使用哪一个,都得到了相同的结果 error/output
cmake_minimum_required(VERSION 3.21)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
add_executable(myProject main.cpp)
target_link_libraries(myProject PUBLIC Python3::Python)
main.cpp
#include <python3.9/Python.h>
#include <iostream>
#include <string>
using namespace std;
// the main in the provided code is void but Clion gave me an error saying to change it to int
int main()
{
cout << "Start 1 \n";
Py_Initialize();
cout << "2\n";
PyObject* my_module = PyImport_ImportModule("myPython");
cerr << my_module << "\n";
PyErr_Print();
cout << "3\n";
PyObject* my_function = PyObject_GetAttrString(my_module,
"printsomething");
cout << "4\n";
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
return 0;
}
myPython.py
import re
import string
def printsomething():
print("Hello from Python!")
预期输出是
Start 1
2
01592AE0 // this will vary since it's the .py files location
3
4
Hello from Python!
我 运行 得到的是
Start 1
2
0 // output is red
3
ModuleNotFoundError: No module named 'myPython' // output is red
由于我不熟悉 CMake,并且在涉及 Ubuntu 时会认为自己是“普通用户”,因此我可能需要有关所提供的任何步骤的一些额外详细信息。
在 Py_Initialize();
PySys_SetPath(L".:/usr/lib/python3.8");
这是 python 模块的搜索路径。添加.
在当前路径搜索