Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Embedded C++ C Python
So, I have two projects: a library and a console app. In the library, I have the following function declaration:
 
std::string NewApp::Testing()
{
	char* test[] = {"test", 0};
	PyObject* obj = Python::Py_Arg("test", "test", test);
	std::string s = PyString_AsString(obj);
	return s;
}
 
Then, in the app, I call the function:
 
int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << NewApp::Testing();
	return 0;
}
 
Which all makes a call to the following Python script (which is in the same release directory as the static library):
 
def test(wrd):
    return wrd
 
Yet, for whatever reason, I get the following error every time I run the program:
 
AttributeError: `module` object has no attribute `test`
Cannot find function "test"
 
And it's driving me crazy! It knows where the file is but it can't find the function. What could be causing it?? I've gotten embedded Python to work before in another app, yet I still can't get this one to work! Any help is appreciated. Thanks.
Posted 11-Jun-13 14:54pm
Comments
Ron Beyer at 11-Jun-13 20:34pm
   
I can't find anything on Py_Arg, are you using this api? http://docs.python.org/2/c-api/arg.html?highlight=pyarg#PyArg_Parse
 
Are you sure you got the function arguments right? Where are you telling the argument that it should be expecting a string?
RadXPictures at 11-Jun-13 22:22pm
   
I followed this tutorial: http://docs.python.org/2/extending/embedding.html. My code is identical, except I replaced the args[] with actual string variable names.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I can see something here:
 
std::string NewApp::Testing()
{
    char* test[] = {"test", 0};//Here you have a char* array called 'test' set to the string of characters "test", ending with the null character '0'///
    PyObject* obj = Python::Py_Arg("test", "test", test);// Here you have 3 tests in your Py_Arg, one of which is the char* array.///
    std::string s = PyString_AsString(obj); // string s now = "test", "test", "test".
    return s;
}
 
And here you 'define' "test" again:
 
def test(wrd):
    return wrd
Do you know what type 'wrd' is? While I'm not Python literate, this line of code is not working as you think. Are you defining a function, a variable, a list, ???
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100