Hello,
I want to call the function from a python file.
By googling, it looks like I need to use the following function.
I added a "Sample.py" in the same place where the exe is stored for the c++ .

int main()
{
    Py_Initialize();
    // Create some Python objects that will later be assigned values.

    PyObject* pName, * pModule, * pFunc, * pArgs = nullptr, * pValue; 
    pName = PyUnicode_FromString((char*)"Sample"); 
    pModule = PyImport_Import(pName); 
    pFunc = PyObject_GetAttrString(pModule, (char*)"fun"); 
    pValue = PyObject_CallObject(pFunc, pArgs);
}

In the "Sample.py"

import matplotlib.pyplot as plt

def fun(): x = [1,2,3]
y = [2,4,1]
plt.plot(1, 2)

And when i run the c++ code, it throws an exception at PyObject_CallObject(pFunc, pArgs); saying "v was nullptr"

I just want to know if im missing any thing in my visual studio setup

Because when i change the sample.py to just following without the #import, it works fine

def fun():  print("Hello from a function")

works fine

thanks a lot

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, y)

    # naming the x axis
    plt.xlabel('x - axis')
    # naming the y axis
    plt.ylabel('y - axis')

    # giving a title to my graph
    plt.title('My first graph!')

    # function to show the plot
    plt.show()
commented: Thanks for the update. I see a lot more work than up top. Good going. Also, mark as solve if you can. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.