One more good news
Code:
#include <iostream>
#include "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\include\Python.h"
using namespace std;
int main(int)
{
Py_Initialize();//-Initialize python interpreter
if (!Py_IsInitialized())
{
PyRun_SimpleString("print 'inital error!' ");
return -1;
}
//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']");
PyObject* pName = NULL;
PyObject* pMoudle = NULL;//---Store the python module to be called
PyObject* pFunc = NULL;//---Store the function to be called
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, NULL);//---Call hello function
Py_Finalize();//---Clean up the python environment and release resources
return 0;
}
Sample.py
Code:
import matplotlib.pyplot as plt
import numpy as np
def fun():
x = np.arange(-5, 5, 0.02)
y = np.sin(x)
plt.axis([-np.pi, np.pi, -2, 2])
plt.plot(x, y, color="r", linestyle="-", linewidth=1)
plt.show()
I changed the function with one of the google suggestions and it finally works !!!
Thanks a lot Arjay for your inputs helped me