win64下C++调用python脚本

#include <iostream>

#include <Python.h>

using namespace std;

int main()
{
	// D:\Python27\include\pyconfig.h
	// 修改 https://blog.csdn.net/Chris_zhangrx/article/details/78947526
	// 修改为 x64

	Py_Initialize();
	if (!Py_IsInitialized())
		return -1;

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('E://tCode//testCpp//scripts//')");

	PyObject* pModule = PyImport_ImportModule("MyPython");      //MyPython:要调用的python文件名
	if (!pModule)
	{
		std::cout << "Cannot open Python file!\n";
		return false;
	}

	PyObject* pFunc = PyObject_GetAttrString(pModule, "printHello");	// 要调用的函数名
	if (!pFunc)
	{
		std::cout << "Failed to get this function!";
		return false;
	}

	PyEval_CallObject(pFunc, NULL);		// 调用函数

	Py_Finalize();

	getchar();

	return 0;
}

MyPython.py

#coding=utf-8

def printHello():
	print("Hello World!")

猜你喜欢

转载自blog.csdn.net/a731062834/article/details/82531951
今日推荐