On 1 December 2017 at 18:37, Nick Coghlan
On 1 December 2017 at 18:13, Neil Schemenauer
wrote: I have been working on reducing Python statup time. It would be nice if there was some way to load a module into memory without exec of its body code. I'm sure other people have wished for this.
Perhaps there could be a new special function, similar to __import__ for this purpose. E.g. __load_module__(). To actually execute the module, I had the idea to make module objects callable, i.e. tp_call for PyModule_Type. That's a little too cute though and will cause confusion. Maybe instead, add a function attribute to modules, e.g. mod.__exec__().
I have a little experimental code, just a small step:
https://github.com/nascheme/cpython/tree/import_defer_exec
We need importlib to give us the module object and the bytecode without doing the exec().
What does actually doing the load give that simply calling https://docs.python.org/3/library/importlib.html#importlib.util.find_spec doesn't?
At that point, you know the module exists, and how to load it, which is all a lazy loading implementations really needs to be confident that a subsequent actual execution attempt will be able to start.
After posting this, and while filing https://bugs.python.org/issue32192, I double checked how "importlib.util.module_from_spec" works, and it turns out that already handle the main part of what you're after: it creates the module without executing it. The actual execution is then handled by running "module.__spec__.loader.exec_module(module)". Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia