Python embed

I am trying to call python functions from C code, and I followed the example here

I also have the correct included file directives, library directives, and python32.lib related (im using python 32), however the error was that python / C APIs like PyString_FromString, PyInt_FromLong, PyInt_AsLong are undefined ( error in the debugger)

This is strange because im also uses other APIs, but they are all fine ...

What is the problem?

int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; if (argc < 3) { fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); return 1; } Py_Initialize(); pName = PyString_FromString(argv[1]); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pDict = PyModule_GetDict(pModule); /* pDict is a borrowed reference */ 

Py_Initialize (), PyImport_Import (), PyModule_GetDict () all work fine, but not PyString_FromString ...

+7
source share
1 answer

Sample code that you used for the ancient version of Python, 2.3.2. The Python 3.x line introduced a number of incompatible not only in the language, but also in the API C.

The functions you are talking about simply no longer exist in Python 3.2.

PyString_ functions have been renamed to PyBytes_ .

PyInt_ functions have disappeared, use PyLong_ .

Here is the same example that you used, but for Python 3:

5.3. Net investment

Note that instead of PyString_/PyBytes_ , PyUnicode_ used. In many places where Python 2.x used byte strings, Python 3.x uses Unicode strings.

By the way, I usually use this page to search for all possible calls:

Index - P

+18
source

All Articles