2

Question

If I have import statements nested in an if/else block, am I increasing efficiency? I know some languages do "one passes" over code for import and syntax issues. I'm just not sure how in depth Python goes into this.

My Hypothesis

Because Python is interpreted and not compiled, by nesting the import statements within the else block, those libraries will not be imported until that line is reached, thus saving system resources unless otherwise needed.

Scenario

I have written a script that will be used by both the more computer literate and those are lesser so. My department is very comfortable with running scripts from the command line with arguments so I have set it up to take arguments for what it needs and, if it does not find the arguments it was expecting, it will launch a GUI with headings, buttons, and more verbose instructions. However, this means that I am importing libraries that are only being used in the event that the arguments were not provided.

Additional Information

  • The GUI is very, very basic (A half dozen text fields and possibly fewer buttons) so I am not concerned with just creating and spawning a custom GUI class in which the necessary libraries would be imported. If this gets more complicated, I'll consider it in the future or even push to change to web interface.
  • My script fully functions as I would expect it to. The question is simply about resource consumption.
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Nathan Smith
  • 683
  • 1
  • 10
  • 24
  • 1
    It sounds like a valid hypothesis, I'm assuming if the else: is called, you `import somePackage`, then once the library is used, `del somePackage` to clear up the resources. – J0hn Jul 07 '17 at 15:38
  • 3
    @J0hn: `del somePackage` won't clear up anything; on first import, the module is cached (so subsequent imports don't have to reimport it), so the resources are still held. All `del somePackage` would do is remove one reference to it. – ShadowRanger Jul 07 '17 at 15:39
  • 3
    I reopened this, since [the other question](https://stackoverflow.com/q/477096/364696) was about delayed/on-demand imports, while this one is about conditional imports (admittedly similar). I think it deserves to be linked, but not classed as a duplicate. – ShadowRanger Jul 07 '17 at 15:46

1 Answers1

4

import statements are executed as they're encountered in normal execution, so if the conditional prevents that line from being executed, the import doesn't occur, and you'll have avoided unnecessary work.

That said, if the module is going to be imported in some other way (say, unconditionally imported module B depends on A, and you're conditionally importing A), the savings are trivial; after the first import of a module, subsequent imports just get a new reference to the same cached module; the import machinery has to do some complicated stuff to handle import hooks and the like first, but in the common case, it's still fairly cheap (sub-microsecond when importing an already cached module).

The only way this will save you anything is if the module in question would not be imported in any way otherwise, in which case you avoid the work of loading it and the memory used by the loaded module.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271