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@gmail.com> wrote:
On 1 December 2017 at 20:17, Steven D'Aprano <steve@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).


Nick Coghlan   |   ncoghlan@gmail.com   |   Brisbane, Australia
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/