[Python-ideas] Provide a way to import module without exec body

Nick Coghlan ncoghlan at gmail.com
Fri Dec 1 03:52:52 EST 2017

On 1 December 2017 at 18:37, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 1 December 2017 at 18:13, Neil Schemenauer
> <nas-python-ideas at arctrix.com> 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


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list