c++调用python(简单版)

来源:互联网 发布:陈漫离婚 知否 编辑:程序博客网 时间:2024/04/27 19:46

声明:这种方法在使用系统自带的python时很有效,但是换个环境,就会出现各种问题;具体问题出现的原因我不在这里叙述,如果你尝试了下述方法,不能成功,那就请点击这篇文章 c++调用python(复杂版)!

Step1:Create the Visual Studio Project

Open Visual Studio and create a new C++ “Win32 Console Application” project.

Step2:Change the build mode from “Debug” to “Release”

If you attempt to build a Visual Studio project that calls Python code in Debug mode the build will fail because, in debugging mode, Visual Studio will look for a library file that does not exist. The reason why this file did not exist on my machine is because the Windows Python installer did not install a version of Python compiled in Debug mode. It is possible to get a version of Python built in Debug mode, but you will have to download the Python source code and build it manually. For the purposes of this blog post we will ignore this and just change to Release mode.

Step3:Add the locations of the Python header and library files to the project properties

My Python installation directory was “C:\Python27”, so I added “C:\Python27\include”to the list of include directories and “C:\Python27\libs” to the list of library directories.

Step4:Add the line “#include <Python.h>” to the C++ source file

Here is an example code:

#include "stdafx.h"#include <iostream>#include <Python.h>int _tmain(int argc, _TCHAR* argv[]){    printf("Calling Python to find the sum of 2 and 2.\n");    // Initialize the Python interpreter.    Py_Initialize();    // Create some Python objects that will later be assigned values.    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;    // Convert the file name to a Python string.    pName = PyString_FromString("Sample");    // Import the file as a Python module.    pModule = PyImport_Import(pName);    // Create a dictionary for the contents of the module.    pDict = PyModule_GetDict(pModule);    // Get the add method from the dictionary.    pFunc = PyDict_GetItemString(pDict,"add");    // Create a Python tuple to hold the arguments to the method.    pArgs = PyTuple_New(2);    // Convert 2 to a Python integer.    pValue = PyInt_FromLong(2);    // Set the Python int as the first and second arguments to the method.    PyTuple_SetItem(pArgs, 0, pValue);    PyTuple_SetItem(pArgs, 1, pValue);    // Call the function with the arguments.    PyObject* pResult = PyObject_CallObject(pFunc, pArgs);    // Print a message if calling the method failed.    if (pResult == NULL)        printf("Calling the add method failed.\n");    // Convert the result to a long from a Python object.    long result = PyInt_AsLong(pResult);    // Destroy the Python interpreter.    Py_Finalize();    // Print the result.    printf("The result is %d.\n", result);    std::cin.ignore();    return 0;}

Step5: Create the Python File

Sample.py

def add(a, b):    return a+b

Add this file to the Release directory of the Visual Studio solution. In general it should be in the same directory as the executable that is calling the Python code.


Ref

CALLING PYTHON CODE FROM C++

0 0
原创粉丝点击