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

Steven D'Aprano steve at pearwood.info
Fri Dec 1 05:17:46 EST 2017

On Fri, Dec 01, 2017 at 02:13:37AM -0600, 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.

I don't understand why you would want to do this. Given a source file:

# module.py
spam = 1
eggs = 2

if you import the module without executing the code in the module, 
surely you'll get a bare module with nothing in it? Then:


will both fail with AttributeError.

If that's what you mean, then no, I haven't wished for that. Unless I'm 
missing something, it seems pointless. When, and why, would I want to 
import an empty module?


More information about the Python-ideas mailing list