#mytest.py you = 'world!' print 'aaaaaaaa' def hello(): print 'hello,world' hello() c++调用 #include <Python.h> int main() { Py_Initialize(); PyObject *pModule = NULL; PyObject *pFunc = NULL; PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); pModule = PyImport_ImportModule("mytest"); pFunc = PyObject_GetAttrString(pModule, "hello"); PyEval_CallObject(pFunc, NULL); Py_Finalize(); return 0; }