简体   繁体   中英

Embed Python in C++ app regadless if python is installed or not (on windows)

I have an app that runs user scripts, using the example on the python site, I can embed python 3.5

The problem is not so much embeding the code, but rather if/or what version of Python the user might have installed.

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

This assumes that the user has Python 3.5 installed, if they have something else installed, the embeded scripts might not work.

Or even worse the app itself will not load.

If they do not have Python installed at all, then the call to Py_Initialize(); will just crash the app.

So I have a couple of questions, how can I include Python in my app, regardless if they have python installed or not?

Or, how can I test what version of Python they have installed so I do not try and call Py_Initialize(); if they don't have it installed or they don't have the correct one

...how can I test what version of Python they have installed...

See Py_GetVersion .

...how can I include Python in my app, regardless if they have python installed or not?

For complete embedding, there's aa guide: Statically Embedding Python on Windows . In general, you just have to copy the Python DLL + standard libraries folder into your application folder and change search paths (see Py_SetPath & Py_SetPythonHome ) before initialization.

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