Embedding Python into C - importing modules -
I'm having trouble using embedded Python for C - whenever I try to use imported module I get one:
Unsupported exception on 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation location 0x00000004.
error
is in PyObject_GetAttrString () method and documentation is not much help. I have also tried to use tutorials from IBM, but always have the same access Get infringement.
The example code for one of the following tutorials, which I can not seem to work on, is that wrong here?
The C-code (in a main file):
#include & lt; Python H & gt; Int main () {PyObject * strret, * mymod, * strfunc, * strargs; Four * seistrets; Py_Initialize (); Mymod = PyImport_ImportModule ("reverse"); Strfunc = PyObject_GetAttrString (mymod, "rstring"); Strargs = Py_BuildValue ("(S)", "Hello World"); Strret = PyEval_CallObject (strfank, strings); PyArg_Parse (strret, "s", and cstrret); Printf ("reverse string:% s \ n", cstrret); Py_Finalize (); Return 0; }
Python code (in a file called reverse.py, same folder):
def rstring (s): i = len (S) - 1t = '' while (i> gt; -1): T + = s [i] i - = 1 return t
I MSVS2008, Python 2.7 Some references: I am trying to embed a small dragon script which uses open opc, which is in a large number of programs and wants to transfer data between the two. However, I have failed in the Proof of concept exam with basic examples already.
PyImport_ImportModule Calls: This fails and returns
NULL < / Code>. The reason for this is by default, the current directory is not in the search path. Add
PySys_SetPath ("."); // first .. mymod = PyImport_ImportModule ("reverse");
To add the existing directory to the module search path and work your example.
Comments
Post a Comment