Saturday, 15 February 2014

PyObject_GetAttrString C++ function returning NULL: Unable to call Python functions from C++ -



PyObject_GetAttrString C++ function returning NULL: Unable to call Python functions from C++ -

i've started working python c++ , i'm bit confused on why i'm unable phone call functions in python c++.

here current test code in c++:

#include <iostream> #include <python.h> using namespace std; int main() { py_initialize(); pyobject* mymodulestring = pystring_fromstring("test"); pyobject* mymodule = pyimport_import(mymodulestring); if( mymodule ) { pyobject* myfunction = pyobject_getattrstring(mymodule, "hello"); if( myfunction ) { pyeval_callobject( myfunction, null ); } else { fprintf( stderr, "myfunction null" ); } } else { fprintf( stderr, "mymodule null" ); } py_finalize(); cin.get(); homecoming 0; }

here test.py python code:

import sys def hello(): print "hello, world!"

before had more complicated test, ran issue pyobject_getattrstring passed null, wanted create simpler test , still received null after calling pyobject_getattrstring. understanding pyobject_getattrstring gets pyobject* function , phone call after, receiving null there means can't phone call function.

also yes have looked @ https://docs.python.org/2.7/ , tested illustration given in step 5.3 of https://docs.python.org/release/2.6.5/extending/embedding.html#pure-embedding (i'm using 2.7.7 because i'm planning integrate 3ds max ). still runs same issues pyobject_getattrstring.

i'm assuming it's simple error or step i'm missing in c++ or python code.

changing python script name test.py else worked me.

python c++ python-2.7

No comments:

Post a Comment