2

I'm prototyping a Python C extension which should be able to import a large number of python modules very quickly with minimum overhead. My extension will know the module names and their absolute paths based on it's own logic. I'd like to avoid using the built-in import mechanism (i.e. I don't need to look up the sys.path to find the module) and avoid using importlib whatsoever because it's a huge overhead with a hundred modules.

I'm trying to find a clean C-API alternative of this:

from importlib import util
spec = util.spec_from_file_location("stub", "/ars/python/stub.py")
stub = util.module_from_spec(spec)
spec.loader.exec_module(stub)

I've seen recommendations like this

PySys_SetPath("path/with/python/files");
PyObject *pModule = PyImport_ImportModule("filename_without_extension");

This is still searches the sys.path and I need to avoid it.

PyObject *imp = PyImport_ImportModule("imp");
PyObject_CallMethod(imp , "load_source", args, "<my_mod>", "<mod_path>");

This one uses an old imp module and prints deprecation warnings. It's not clear how to replace it with the new importlib since there is no such a high-level load_source method as in the old imp.

Any ideas are welcome, thanks.

Alex
  • 93
  • 1
  • 5

0 Answers0