Click here to Skip to main content
Rate this: bad
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
Ron Beyer at 11-Jun-13 20:34pm
I can't find anything on Py_Arg, are you using this api?
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: My code is identical, except I replaced the args[] with actual string variable names.

1 solution

Rate this: bad
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, ???

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

  Print Answers RSS
0 /\jmot 230
1 Zoltán Zörgő 210
2 Peter Leow 145
3 Sergey Alexandrovich Kryukov 115
4 _Asif_ 108
0 Sergey Alexandrovich Kryukov 9,353
1 OriginalGriff 6,881
2 Peter Leow 4,577
3 Zoltán Zörgő 4,129
4 Richard MacCutchan 2,882

Advertise | Privacy | Mobile
Web04 | 2.8.150129.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2015
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