1

in my C++ application, I want to invoke a Python script to do some processing works. I have searched in Google and found there are two ways to do this:

    (1). use the `system` command. Just like this, `system('python myscript.py');`
    (2). use the `PyRun_SimpleString("execfile('myscript.py')");`

I want to ask that, which way is better and is there any better way to do this work?

Thanks.

mining
  • 3,557
  • 5
  • 39
  • 66

2 Answers2

2

I want to ask that, which way is better and is there any better way to do this work?

You should notice that the python engine is written in C and therefore provides a native C-API. This allows you to interact more directly with python code, by means of calling functions and using python objects.

If you want to integrate it from C++ code without hassling with the C-API, there's the excellent and easy to use boost::python library.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

I think that boost provides libraries to do that. But I never used them. http://www.boost.org/doc/libs/1_55_0/libs/python/doc/

Caduchon
  • 4,574
  • 4
  • 26
  • 67