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

brent bejot brent.bejot at gmail.com
Fri Dec 1 10:23:37 EST 2017


I have found myself implementing something like this before.  I was working
on a command-line tool with nested sub-commands.  Each sub-command would
import a script and execute something out of it.  I ended up moving the
importing of those little scripts into the functions that called them
because importing all of them was slowing things down.  A built-in lazy
importer would have made for a better solution.

On Fri, Dec 1, 2017 at 5:36 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 1 December 2017 at 20:17, Steven D'Aprano <steve at pearwood.info> wrote:
> > 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?
>
> Having access to something along these lines is the core building
> block for lazy loading. You figure out everything you need to actually
> load the module up front (so you still get an immediate ImportError if
> the module doesn't even exist), but then defer actually finishing the
> load to the first __getattr__ invocation (so if you never actually use
> the module, you avoid any transitive imports, as well as any other
> costs of initialising it).
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171201/894d3d2f/attachment.html>


More information about the Python-ideas mailing list