win64下C++呼叫python指令碼
阿新 • • 發佈:2019-01-06
#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!")