I'm trying to embed a python script file in my c++ program. I have included Python.h
and it works as expected.
#include "MathFunction.h"
#include <Python.h>
int main() {
char filename[] = "TestPy.py";
Py_Initialize();
FILE *fp;
fp = _Py_fopen(filename, "r");
PyRun_SimpleFile(fp, filename);
Py_Finalize();
return 0;
}
Note: I have tried using fopen
, instead of _Py_fopen
, but it has the same result.
But, when I run this code, I get an exit code of 0 (good), but I get no output from my TestPy.py
, which has:
import matplotlib.pyplot as plt
l1 = [1, 2, 3, 4]
l2 = [3, 6, 4, 5]
plt.plot(l1)
plt.show()
I was expecting a plot to open, as it does when running it natively in Python.
Even when the python
file is as simple as
Print("Hello!"), I don't see the print statement in the console.
I don't get any console output. Although, when I use
PyRun_SimpleString("print('Hello!')");
Instead of PyRun_SimpleFile
, I get console output of "Hello!", as expected. Any reason why this might be?
TestPy.py
is in the same directory as the c++ file.