67

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636

7 Answers7

77

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
emmenlau
  • 958
  • 12
  • 20
bhadra
  • 12,887
  • 10
  • 54
  • 47
  • 1
    This does not work with any Visual studio version later than 2008, since thats what python for windows is compiled with. It can be possible if you find or are able to yourself compile a version of python using a later runtime. Ive just spent a day trying to compile python using vs 2013 without any success. – David Apr 29 '14 at 16:01
  • I was able to build a library for embedding using VS 2015. It is not that hard to do, but figuring out the steps was frustrating. https://stackoverflow.com/questions/48545255/i-cannot-build-python-dll-as-a-static-library-mtd-using-visual-studio/49984841#49984841 – Jiminion Apr 26 '18 at 14:28
  • *"This means that you want to embed Python in your C++ application..."* - not necessarily. He may well want to start an external process and wait for the process. – jww Apr 01 '19 at 20:56
  • 2
    final link broken – joel Nov 30 '19 at 17:19
39

Boost has a python interface library which could help you.

Boost.Python

cp.engr
  • 2,291
  • 4
  • 28
  • 42
roo
  • 7,106
  • 8
  • 39
  • 45
  • 1
    https://github.com/boostorg/python/blob/develop/example/quickstart/embedding.cpp – slf Nov 04 '21 at 17:06
32

Interestingly, nobody has mentioned pybind11, yet. From their documentation:

pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.Python library by David Abrahams: to minimize boilerplate code in traditional extension modules by inferring type information using compile-time introspection. [...] Since its creation, this library has grown beyond Boost.Python in many ways, leading to dramatically simpler binding code in many common situations.

Concretely, calling into a Python function (called embedding) is as simple as this (taken from the documentation):

#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive
    py::print("Hello, World!"); // use the Python API
}
ingomueller.net
  • 4,097
  • 2
  • 36
  • 33
9

Use system call to run a python script from C++

#include<iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int result = system("/usr/bin/python3 testGen1.py 1");
cout << result; 
}
RiteshC
  • 301
  • 4
  • 4
  • 1
    It works for me when using the built-in modules from python. However, when importing a module (e.g., numpy) inside the python file it is not able to imported. Any idea of how to tackle this? – Mauricio Arboleda-Zapata Feb 18 '21 at 08:01
3

Embeding the Python interpreter inside your C++ app will let you run Python programs using your application run Python scripts. It will also make it easier possible for those scripts to call C++ functions in your application. If this is what you want then the Boost library mentioned previously may be what you want to make it easier to create the link. In the past I have used SWIG to generate Python interfaces to C++ code. It was not clear from your question whether you wanted the Python scripts to call your C++ program or whether you just wanted the C++ to call Python.

Many of the Python functions use modules which are not built into the Python interpreter. If your Python scripts call these functions then you will either need to have your users install Python or include the python runtime files with your application. It will depend on what modules you import in you Python scripts.

ljk321
  • 16,242
  • 7
  • 48
  • 60
David Dibben
  • 18,460
  • 6
  • 41
  • 41
2

Boost is probably the best choice, however if you're wanting something that's more standalone, and if this is for use with Windows (which seems feasible given that they are the people least likely to have Python installed), then you can use py2exe to create a DLL with entry points suitable for COM objects. You can then interface with the library via COM. (Obviously this is not at all useful as a cross-platform solution).

Tony Meyer
  • 10,079
  • 6
  • 41
  • 47
-1

Using Inter Process Communication (IPC) over socket can be a possible solution. Use a local network socket to listen/trasfer commands between both.

Yahya Tawil
  • 395
  • 4
  • 10