Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling python from a c++ program for distribution

I would like to call python script files from my c++ program.

I am not sure that the people I will distribute to will have python installed.

Basically I'm looking for a .lib file that I can use that has an Apache like distribution license.

like image 464
Brian R. Bondy Avatar asked Sep 08 '08 03:09

Brian R. Bondy


People also ask

How do you call a Python script from C program?

Create the return value, Restore previous GIL state and return. A reference to an existing Python callable needs to be passed in, to use this function. To do that there are many ways like – simply writing C code to extract a symbol from an existing module or having a callable object passed into an extension module.

Can a C++ program run a Python script?

It is also possible to embed Python in a C++ program; precisely how this is done will depend on the details of the C++ system used; in general you will need to write the main program in C++, and use the C++ compiler to compile and link your program. There is no need to recompile Python itself using C++.

Can C call Python function?

We can call a C function from Python program using the ctypes module.


1 Answers

I would like to call python script files from my c++ program.

This means that you want to embed Python in your C++ application. As mentioned in Embedding Python in Another Application:

Embedding Python is similar to extending it, but not quite. The difference is that when you extend Python, the main program of the application is still the Python interpreter, while if you embed Python, the main program may have nothing to do with Python — instead, some parts of the application occasionally call the Python interpreter to run some Python code.

I suggest that you first go through Embedding Python in Another Application. Then refer the following examples

  1. Embedding Python in C/C++: Part I

  2. Embedding Python in C/C++: Part II

  3. Embedding Python in Multi-Threaded C/C++ Applications

If you like Boost.Python, you may visit the following links:

  1. Embedding Python with Boost.Python Part 1
like image 198
bhadra Avatar answered Sep 25 '22 00:09

bhadra