13

I am trying to understand few things with respect to design.

I see a number of the code where Lazy Import features is used.By Lazy Import, I mean a facility provided by certain recipes, packages and modules which support "LazyImport" style. Those implementation in general aim to import the module only when it is used and provide some extra hooks for different things. I know there the error condition is delayed over here, but I am trying to understand why Lazy Import is not a default strategy in Python.

What could it's (other) drawbacks be which prevent it from making a general useful case. Or are there languages which use this as a default import mechanism strategy.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131

3 Answers3

5

Here's an example of lazy import in python:

def xmlfrobnicator(xmlstr):
    from lxml import etree

    # do whatever

It's not commonly used because it offers very few advantages for most programmes - once loaded, the module is loaded (unless you take steps to unload it), and it's pretty rare to have a dependency that is used so infrequently that only loading it when in use is worthwhile.

I think you may have been looking at javascript, where programmes may only run for a short time, and not use all of their features, and even if they do, background loading of modules improves user-perceived speed.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • 1
    The folks who use LazyImport say that they "have the a dependency that is used so infrequently that only loading it when in use is worthwhile" - I looked the implementations that provide LazyImport like PEAK's Importing - looks fine and not at all complicated. – Senthil Kumaran Apr 10 '12 at 07:42
  • @SenthilKumaran If you don't want to hear the answer, don't ask the question. As you note in your question, most python users don't use lazy importing, and that is for the reason that I give. – Marcin Apr 10 '12 at 07:44
  • Hello Marcin - I was not trying to be rude. In fact, I had to work with couple of packages which used LazyImport and I had to get my head around it. Now that i got it, I asked why is not a default. (I am trying to understand technical drawbacks, if any) – Senthil Kumaran Apr 10 '12 at 07:47
  • And alternate thought I had was - is the application that I am working with do away with LazyImport module and component based architecture. So the question is pretty serious and I wanted to know good answers. – Senthil Kumaran Apr 10 '12 at 07:53
  • @SenthilKumaran You do not require lazy import to have a component-based architecture. If you are using lazy import, go ahead and use it. Just because it's useful for one application doesn't mean that everyone else should be using it. – Marcin Apr 10 '12 at 08:00
  • I am sorry, I think either I am not posing the correct question or you are not getting it. From my question - "What could it's (other) drawbacks be which prevent it from making a general useful case. " – Senthil Kumaran Apr 10 '12 at 08:04
  • @SenthilKumaran And I have answered your question: It offers no advantages where it is not necessary. – Marcin Apr 10 '12 at 08:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9894/discussion-between-senthil-kumaran-and-marcin) – Senthil Kumaran Apr 10 '12 at 08:14
  • The comment got automatically added. Anyways, Thanks for your answers. – Senthil Kumaran Apr 10 '12 at 08:26
  • @SenthilKumaran I know how those comments are added - I have been using the site as long as you have. – Marcin Apr 10 '12 at 08:30
  • 1
    I clicked on "Would you like to automatically move this discussion to chat?" and I posted my thoughts there and it added that as comment. FWIW, I used it for the first time. :) – Senthil Kumaran Apr 10 '12 at 08:33
5

Python, unlike e.g. PHP, is rarely used in a way where every request/action/... causes the whole application to be started again.
So importing everything at startup has the advantage of not having to perform imports while the application is doing something where delays are annoying.
The only advantage of local/lazy imports is that you won't have problems with circular imports.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3

Sometimes modules do important things when they are first loaded, so it might break the program to delay import of the module. For example, if a module defines command-line flags that should be parsed when the program first starts, the module must be imported before parsing the arguments. Since Python was originally designed to do imports eagerly, it's not possible to change the default behavior now without breaking some existing programs. Also, as mentioned in some of the other answers, for long-running services it's often desirable to load everything up front before serving requests so that the first few requests are not slowed down waiting for modules to be imported.

  • This is the only correct answer to the "why" question asked. However, python should certainly support something like "import 'lazy'" as a first-class feature... rather than requiring programmers to stuff import statements into function calls. – Erik Aronesty May 02 '18 at 19:57
  • Another reason is that sometimes you want to know if a module is broken *before* the program gets to the function call in it. – Erik Aronesty May 02 '18 at 19:58