agrafft
agrafft

Reputation: 51

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

I've just started working with Python with C++ and I'm a bit confused on why I'm unable to call functions in Python from C++.

Here is my 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 is NULL" );
                      }
             }
             else
             {
                      fprintf( stderr, "myModule is NULL" );
             }

             Py_Finalize();
             cin.get();
             return 0;
      }

Here is my test.py Python code:

      import sys

      def Hello():
            print "Hello, world!"

Before I had a more complicated test, but I ran into an issue where PyObject_GetAttrString passed back NULL, so I wanted to make a simpler test and I still received NULL after calling PyObject_GetAttrString. From my understanding PyObject_GetAttrString gets you the PyObject* to the function and then I call it after, so receiving NULL there basically means I can't call the function.

Also yes I have looked at https://docs.python.org/2.7/ and even tested the example 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 to integrate with 3ds Max ). It still runs into the same issues with PyObject_GetAttrString.

I'm assuming it's a simple mistake or step I'm missing in the C++ or Python code.

Upvotes: 5

Views: 4998

Answers (3)

James P
James P

Reputation: 51

I had the same problem. Although test.py and mycode.c were in the same folder (/home/user/python/example), I had to add a reference to the path after Py_Initialize(); as shown in following line

PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/user/python/spp')");*

Replace /home... to your path.

My previous code did not have that line of code but PyImport_Importwas working and PyObject_GetAttrString wasn't. Makes no sense to me, "don't ask me, I don't know - Ozzy".

Upvotes: 2

Derek
Derek

Reputation: 1

I was having a similar problem. I found that the whitespace in the string I was passing to python was way off. From what I could tell, your print statement has 6 spaces in lieu of 4. Back it up and see if everything doesn't clear up.

Upvotes: 0

Ryan
Ryan

Reputation: 53

Changing the Python script name from test.py to anything else worked for me.

Upvotes: 3

Related Questions