C/C++執行Python代碼

Python腳本:python

import sys


def start():
    try:
        print('Hello World')
    except Exception as e:
        print(e.args)

C/C++示例:ios

#include <iostream>
#include <Python.h>

using namespace std;

int main()
{
	Py_Initialize();
	if (!Py_IsInitialized())
	{
		cout << "Py_IsInitialized() fail" << endl;
		return 0;
	}
	
	PyRun_SimpleString("import hello");
	PyRun_SimpleString("hello.start()");

	Py_Finalize();

	cout << "exit" << endl;
	getchar();


	return 0;
}
相關文章
相關標籤/搜索