0

I am trying to embedded a python script within a C++ application. I am trying to do it without having the need to use the argv parameters and therefore I am trying to follow this tutorial: https://www.codeproject.com/Articles/820116/%2FArticles%2F820116%2FEmbedding-Python-program-in-a-C-Cplusplus-code

My first step was to run this tutorial (1.1 Very High Level embedding): https://docs.python.org/3/extending/embedding.html which is now working.

While going to the second tutorial, my code compiled but the script doesn't seems to be loaded as nothing is displayed in the console

main.cpp:

#include <iostream>
#include <string>
#include <Python.h>

using namespace std;

int main() {
    cout << "Hello, World!" << endl;

    char filename[] = "test_script.py";
    FILE* fp;

    Py_Initialize();

    fp = _Py_fopen(filename, "r");
    PyRun_SimpleFile(fp, filename);

    Py_Finalize();
    return 0;
}

test_script.py:

print("print test from python")

def multiply(a,b):
    print("Will compute", a, "times", b)
    c = 0
    for i in range(0, a):
        c = c + b
    return c

CmakeList.txt

cmake_minimum_required(VERSION 3.13)
project(TestCharacter)

set(CMAKE_CXX_STANDARD 14)

find_package(PythonLibs REQUIRED)

add_executable(TestCharacter main.cpp)

target_include_directories(TestCharacter PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(TestCharacter PRIVATE ${PYTHON_LIBRARIES})

As I don't get any building error I don't know where to look for. I have tried to modify the script's name within my C++ code as to force an error (I first thought that it may be a path or name error), but I didn't get any building error

  • 1
    Did you check whether the `_Py_fopen` succeeded? It might have returned `NULL` if it could not find or open the script file. – Botje Jan 09 '19 at 16:28
  • So I just added assert(fp!=NULL) to control if it is NULL and indeed it is. Should I tell the CMakeList.txt to look specifically for the script ? I am not so familiar with C++ – Michael Stettler Jan 09 '19 at 22:19
  • Depends on how you run it, but the easiest solution is to copy the script to your build directory and run your program from there. – Botje Jan 10 '19 at 08:04
  • May I ask you to be more specific on how I could run my program from there ? Currently main.cpp, test_script.py and CMakeLists.txt are already all within the same (build) directory. I am using CLion to build and run my code, I am on a Mac – Michael Stettler Jan 10 '19 at 08:52
  • 1
    CLion will create a _different_ directory for building in. (and presumably running your program from that directory) Hunt around in your project settings, find out by printing the result of `getcwd`, or debug your program and see if you get the full path to it. – Botje Jan 10 '19 at 09:55
  • You were write! Thanks a lot. So CLion was creating this "cmake-build-debug" directory. Moving my python script within this folder solved my issue! – Michael Stettler Jan 10 '19 at 10:42

1 Answers1

0

The issue was due to a wrong building directory access created by CLion as Botje pointed out. By moving my script to the folder: "cmake-build-debug" solved the problem.