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.

asked Mar 24, 2014 at 7:16
2
  • 1
    Depends on what you consider is better ... Commented Mar 24, 2014 at 7:37
  • Use popen() as described here Commented Mar 24, 2014 at 8:35

2 Answers 2

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.

answered Mar 24, 2014 at 7:53

Comments

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/

answered Mar 24, 2014 at 7:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.