How do I access a module written in Python from C?

Technology CommunityCategory: PythonHow do I access a module written in Python from C?
VietMX Staff asked 3 years ago

You can get a pointer to the module object by calling PyImport_ImportModule:

module = PyImport_ImportModule("mymodule");

You can then access the module’s attributes (i.e. any name defined in the module) as follows:

attr = PyObject_GetAttrString(module, "name");

Calling PyObject_SetAttrString to assign to variables in the module also works.