- ?//要点1: 千万别用\\路径,尽量用/
- //要点2: 先导入包路径
- //要点3: cmd进入目录手动测试一下代码
- //要点4 :多用PyErr_Print 就知道为什么错了
Py_Initialize();
?? ?PyRun_SimpleString("import sys"); ?? ?PyRun_SimpleString("sys.path.append('./')"); ?? ?
??PyRun_SimpleString("sys.path.append('C:/Users/Administrator/PycharmProjects/untitled/venv/Lib/site-packages')"); ?? ? PyRun_SimpleString("print(sys.path)");
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs;
??if(!(pName = PyUnicode_FromString("main"))) ?? ??? ?return NULL; ?? ?pModule = PyImport_Import(pName); ? ?? ?? ??? ?PyErr_Print(); ?? ??? ?return -1; ?? ?} ?? ?pDict = PyModule_GetDict(pModule); ?? ?if (!pDict) { ?? ??? ?return -1; ?? ?}
?? ?// 找出函数名为add的函数 ?? ? ?? ?pFunc = PyDict_GetItemString(pDict, "GetBlock"); ?? ?if (!pFunc || !PyCallable_Check(pFunc)) { ?? ??? ?return -1; ?? ?}
? ?pArgs = PyTuple_New(0);
?? ?PyObject * pReturnTuple = PyObject_CallObject(pFunc, pArgs); ?? ? ?? ?const char* pp = PyUnicode_AsUTF8(pReturnTuple);
?? ?Py_Finalize();
|