简体   繁体   中英

Deploying C++ program with python embedded

I have a Qt C++ project in which, for some functionality,I have added Python. The C++ function calls python script and returns the values.Like the below Example.

PyObject *pName,*pModule,*pFunc;
PyObject *pArgs,*pValue;
const char *module="getBeamData";
pName=PyUnicode_FromString(module);
pModule=PyImport_Import(pName);
if(pModule!=NULL){
    pFunc=PyObject_GetAttrString(pModule,"getBeamDose");
    if(pFunc&&PyCallable_Check(pFunc)){
        pArgs=PyTuple_New(2);        
  PyTuple_SetItem(pArgs,0,PyBytes_FromString(path.toStdString().c_str()));
        PyTuple_SetItem(pArgs,1,PyLong_FromLong(fraction));      
        pValue=PyObject_CallObject(pFunc,pArgs);

It calls the script getBeamdata.py. Functionality works fine. Do I have to install python and libraries(like numpy) for all customer and ship script ? Or is there any other way without sending script.

Without statically compiling your Python script you will need to distribute:

  • requirements.txt (for all libraries/modules required by your script).
  • details on the correct version of Python required by the script (2.7/3 etc).

Alternatively you could look into creating an executable using py2exe and distribute that instead, although you'll need to modify the way your program interacts with the Python script.

Some further details on distributing Python can be found here .

If I were you, I'd do it with some sort of installer.

Now in that installer, you can of course embed the wheel version of your script, which is in addition going to be installed using setuptools .

In that setuptools script you can mention dependencies, but that means that your client should have either Python installed on his/her computer, or you should ship your own version of Python embedded with the software.

Such functionality can be also be achieved using Boost.Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM