Calling python from a c++ program for distribution

2019-01-06 09:01发布

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.

5条回答
一纸荒年 Trace。
2楼-- · 2019-01-06 09:45

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
查看更多
迷人小祖宗
3楼-- · 2019-01-06 09:45

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).

查看更多
地球回转人心会变
4楼-- · 2019-01-06 09:57

Boost has a python interface library which could help you.

Boost.Python

查看更多
Bombasti
5楼-- · 2019-01-06 09:57

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.

查看更多
聊天终结者
6楼-- · 2019-01-06 10:01

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
}
查看更多
登录 后发表回答