On Oct 30, 6:47 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > i switched to PyImport_ImportModuleEx per your suggestion but i still > > get > > > Traceback (most recent call last): > > File "<string>", line 1, in ? > > NameError: name '__main__' is not defined > > > i tried PyRun_SimpleString("__main__.foo.baz()"); : > > Traceback (most recent call last): > > File "<string>", line 1, in ? > > NameError: name '__main__' is not defined > > > i do not have if __name__ == '__main__': > > statement anywhere in the module i'm importing, > > but nevertheless i checked that PyImport_AddModule("__main__"); > > and subsequent getdict return non null pointers. > > does non null dictionary indicate that PyImport_AddModule > > succeeded as opposed to creating an empty module object? > > Sorry, I misunderstood the behavior of the PyImport_ImportModuleEx() > function. You can use the PyImport_AddModule() function as before, but > you must explicitly add the module to the __main__ module. Here is some > code: > > PyObject *mainmod = PyImport_AddModule("__main__"); > PyObject *foo = PyImport_ImportModule("foo"); > Py_INCREF(foo); //Increment foo module since PyModule_AddObject() steals > reference > PyModule_AddObject(mainmod, "foo", foo); > PyRun_SimpleString("foo.baz()"); > > -Farshid
it works! could i cache maindict pointer in a global var or am i condemned to call PyObject* mainmod = PyImport_AddModule("__main__"); assert(mainmod); PyObject* maindict = PyModule_GetDict(mainmod); assert(maindict); every time i want to PyRun_String? i call Py_Finalize only atexit. i also found boost library docs of some help: http://service-spi.web.cern.ch/service-spi/external/Boost/1.30.0/rh73_gcc32/libs/python/doc/tutorial/doc/using_the_interpreter.html btw what kind of object is returned in case i use Py_file_input in PyRun_String? -- http://mail.python.org/mailman/listinfo/python-list