In Python, what happens when you import inside of a function?

In Python, what happens when you import inside of a function?

Does it re-import every time the function is run?

No; or rather, Python modules are essentially cached every time they are imported, so importing a second (or third, or fourth…) time doesnt actually force them to go through the whole import process again. 1

Does it import once at the beginning whether or not the function is run?

No, it is only imported if and when the function is executed. 2, 3

As for the benefits: it depends, I guess. If you may only run a function very rarely and dont need the module imported anywhere else, it may be beneficial to only import it in that function. Or if there is a name clash or other reason you dont want the module or symbols from the module available everywhere, you may only want to import it in a specific function. (Of course, theres always from my_module import my_function as f for those cases.)

In general practice, its probably not that beneficial. In fact, most Python style guides encourage programmers to place all imports at the beginning of the module file.

The very first time you import goo from anywhere (inside or outside a function), goo.py (or other importable form) is loaded and sys.modules[goo] is set to the module object thus built. Any future import within the same run of the program (again, whether inside or outside a function) just look up sys.modules[goo] and bind it to barename goo in the appropriate scope. The dict lookup and name binding are very fast operations.

Assuming the very first import gets totally amortized over the programs run anyway, having the appropriate scope be module-level means each use of goo.this, goo.that, etc, is two dict lookups — one for goo and one for the attribute name. Having it be function level pays one extra local-variable setting per run of the function (even faster than the dictionary lookup part!) but saves one dict lookup (exchanging it for a local-variable lookup, blazingly fast) for each goo.this (etc) access, basically halving the time such lookups take.

Were talking about a few nanoseconds one way or another, so its hardly a worthwhile optimization. The one potentially substantial advantage of having the import within a function is when that function may well not be needed at all in a given run of the program, e.g., that function deals with errors, anomalies, and rare situations in general; if thats the case, any run that does not need the functionality will not even perform the import (and thats a saving of microseconds, not just nanoseconds), only runs that do need the functionality will pay the (modest but measurable) price.

Its still an optimization thats only worthwhile in pretty extreme situations, and there are many others I would consider before trying to squeeze out microseconds in this way.

In Python, what happens when you import inside of a function?

It imports once when the function executes first time.

Pros:

  • imports related to the function theyre used in
  • easy to move functions around the package

Cons:

  • couldnt see what modules this module might depend on

Leave a Reply

Your email address will not be published. Required fields are marked *