simple? embedding question
Farshid Lashkari
no at spam.com
Tue Oct 30 20:47:07 EDT 2007
sndive at gmail.com 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
More information about the Python-list
mailing list