Getting module object by name in C extension module
ilariu at yahoo.com
Wed Jul 14 12:56:12 CEST 2004
> I haven't written any C extensions for a while, but looking at the
> documentation this seems to be the way to go:
> PyObject * modules = PyImport_GetModuleDict();
> PyObject * module_object = PyDict_GetItemString(modules, "module_name");
> The first line gets the value of sys.modules, and the second line looks up
> the module name in it. Both of those are borrowed references, so you don't
> need to Py_DECREF() them.
> If the module might not have been loaded, you can do this:
> PyObject * module_object = PyImport_Import("module_name");
> but then you have to Py_DECREF() module_object once you're finished.
More information about the Python-list