Problems Calling Python From C++
Solution 1:
Last I checked, C doesn't have exceptions. Surely, you're not going to get any exceptions thrown by calls to the Python lib.
First, since you're using C++, you may need to include the Python lib with an extern declaration.
extern "C" {
#include "python.h"
}
Next, since you don't have exceptions in C calls, you should test the result of each call as you go along. This will help you better understand where it's failing.
Since you're not getting a segfault or anything, I suspect you're getting to
if(!Py_IsInitialized())
return -1;
And exiting. Instead, you could print the return value so you know what's happening.
int is_init = Py_IsInitialized();
cout << "are we initialized? " << is_init;
if(!is_init)
return -1;
If that doesn't demonstrate the trouble, then add additional cout statements throughout your code to trace where the problem is occurring... or better yet, use a debugger and step through the code as it runs. Surely you'll find what's going wrong.
Solution 2:
I found it contains some chinese words in first line.
#XXX
And, it also didn't work in pythonwin. Said something wrong.
So, I deleted them, and it's OK!
Post a Comment for "Problems Calling Python From C++"