I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os
or shutil
. How is best to do this?

- 123,847
- 25
- 243
- 454

- 16,882
- 23
- 75
- 93
2 Answers
PyObject* os = PyImport_ImportModuleNoBlock("os");
if (os == NULL)
return NULL;
someattr = PyObject_GetAttrString(os, "someattr");
Py_DECREF(os);
If you import the module only once e.g., in init_yourmodule()
function then use PyImport_ImportModule("os")
.

- 399,953
- 195
- 994
- 1,670
-
The function `PyImport_ImportModuleNoBlock` is now [deprecated](https://docs.python.org/3/c-api/import.html#c.PyImport_ImportModuleNoBlock) – Evandro Coan May 26 '19 at 02:37
-
@user yes. Now both functions are the same. – jfs May 28 '19 at 14:50
Don't.
Instead, change your extension module so that it provides a service to Python, and then write Python code which calls os
, shutil
and your module.
In fact, for a lot of the content in the os
module it is probably better to write native C code rather than call into Python.
Of course, you can call Python modules from C code, it's just that doing that is overkill for low level modules like os
and shutil
. When it comes to the file copying methods in shutil
reimplementing them in your C code is trivial. In fact, on Windows, copying a file is done by a call to the OS so there is not much code to even write in C.
If the Python module is written in C you could even just copy the code for the methods that you need.

- 31,973
- 6
- 70
- 106
-
2Complex extension modules sometimes need to do stuff that was already implemented in stdlib. It's a shame to reimplement them in C, or to restructure the code – zaharpopov Feb 11 '12 at 07:37