PyRun_File / PyRun_String

Ulrich Huenemoerder uhuenemoerder at hotmail.com
Mon Nov 8 22:41:59 CET 1999


Hello again,


if somebody wants to know, this is how I solved it....

(I found out I have to use compiler option /MD using Visual C++ 6.0, too,
else the program crashed)

Ulrich.


#
# /my/script/path/myTest.py
#
def doPrint():
    print "called doPrint"

if __name__ == '__main__':
    print "this is __main__"

# end of myTest.py

//
// /somewhere/else/main.cpp
//
void main(int argc, char **argv) {

    /* variables */
    PyObject *pdict;

    Py_Initialize();

    pdict = PyDict_New();
    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());

    // extend the searchpath
    PyRun_String("import sys", file_input, pdict, pdict);
    PyRun_String("sys.path.insert(0, '/my/script/path')", file_input, pdict,
pdict);

    // import the script -> no need for PyRun_File !!!
    PyRun_String("import myTest", file_input, pdict, pdict);

    // call the function defined in myTest.py
    PyRun_String("myTest.doPrint1()", file_input, pdict, pdict);
}

// end of main.cpp






More information about the Python-list mailing list