Getting module object by name in C extension module
Ilariu Raducan
ilariu at yahoo.com
Wed Jul 14 06:56:12 EDT 2004
Thank You,
Ilariu
>
> 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.
>
> Nick
>
>
More information about the Python-list
mailing list