简体   繁体   中英

Implement Python Scripting Into C++

I'm creating an application in Python with dynamic scripting that allows users to create their own Python scripts and manipulate the application. I would much rather make the application in c++ but I also want the custom Python script manipulation. So... basically I need c++ to read and execute Python scripts. Is there a way to do this?

Yes, you'll want to look at Embedding Python in Another Application . Here's the example from "Very High Level Embedding" from the python docs.

#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;
}

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