Thankyou for the reply and help. Btw, i already got this working.
#include <iostream>
#include "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\include\Python.h"
#include "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Lib\site-packages\numpy\core\include\numpy\arrayobject.h"
#include <vector>
using namespace std;
int main(int)
{
Py_Initialize();//-Initialize python interpreter
if (!Py_IsInitialized())
{
PyRun_SimpleString("print 'inital error!' ");
return -1;
}
import_array();
//PyRun_SimpleString("print 'inital ok! ");
PyRun_SimpleString("import sys");//--It is equivalent to the import sys statement in python, sys is to deal with the interpreter
PyRun_SimpleString("sys.path.append('./')"); //Specify the directory where pytest.py is located
PyRun_SimpleString("sys.argv = ['python.py']");
int fArray1[5] = { 5, 2, 9, 4, 7 };
npy_intp m[1] = { 5 };// Initialize the Python Interpreter
PyObject* c1 = PyArray_SimpleNewFromData(1, m, NPY_INT, fArray1);
int fArray2[5] = { 10, 5, 8, 4, 2 };
PyObject* c2 = PyArray_SimpleNewFromData(1, m, NPY_INT, fArray2);
// Create Tupe of size 2 to pass two arguements
PyObject* pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, c1);
PyTuple_SetItem(pArgs, 1, c2);
PyObject* pName = NULL;
PyObject* pMoudle = NULL;//---Store the python module to be called
PyObject* pFunc = NULL;//---Store the function to be called
PyObject* pArgTuple = NULL;
pName = PyUnicode_FromString("Sample"); //Specify the file name to be imported
pMoudle = PyImport_Import(pName);//--Using the import file function to import the helloWorld.py function
if (pMoudle == NULL)
{
PyRun_SimpleString("print 'PyImport_Import error!' ");
return -1;
}
pFunc = PyObject_GetAttrString(pMoudle, "fun");//--Find the hello function in the python reference module helloWorld.py
PyObject_CallObject(pFunc, pArgs);//---Call hello function
Py_Finalize();//---Clean up the python environment and release resources
return 0;
}
Python Script in Sample.py file:
import matplotlib.pyplot as plt
import numpy as np
def fun(x,y):
plt.plot(x, …