2

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.

trumanf_
  • 23
  • 5
  • The second script using `matplotlib` does produce any console output — what are you expecting? – martineau Dec 25 '20 at 00:05
  • @martineau I edited my post slightly to reflect what I was expecting, thank you. I am expecting the plot to open as it would in Python, but I'm also confused why doing a simple Print() doesn't output to the console. – trumanf_ Dec 25 '20 at 00:09
  • Works perfectly for me, the program opens a window with a plot. – n. m. could be an AI Dec 25 '20 at 00:31
  • @n.'pronouns'm. I'm building/running using CLion on Windows... could that have anything to do with it? Are you on Linux? – trumanf_ Dec 25 '20 at 00:33
  • "TestPy.py is in the same directory as the c++ file." Location of your C++ file is irrelevant. Indeed, most projects have many C++ files in many different directories, how could they all possibly be relevant? You need to familiarise yourself with the concept of *current working directory*. `TestPy.py` is a *relative path*. Relative with respect to what? To the current working directory. So you need to look in your project settings and figure out what the current working directory is when running your program. – n. m. could be an AI Dec 25 '20 at 00:43
  • @n.'pronouns'm. With a quick google on current working directory, I managed to fix it within my IDE's settings. I had no idea there was a distinct difference between current and current _working_ directory. Thank you. – trumanf_ Dec 25 '20 at 00:54
  • No, they are the same thing, there is no difference. – n. m. could be an AI Dec 25 '20 at 08:49

1 Answers1

0

We had the same issue on windows (on linux the code you put should work).

Following this answer was the good one : Why does the Python/C API crash on PyRun_SimpleFile?

PyObject *obj = Py_BuildValue("s", "test.py");
FILE *file = _Py_fopen_obj(obj, "r+");
if(file != NULL) {
    PyRun_SimpleFile(file, "test.py");
}

TLDR : onwindows fopen didn't work, _Py_fopen_obj worked

ice3
  • 335
  • 3
  • 15