I am often tempted to import modules in narrow contexts where they are needed. For example in the body of a function that utilises the module. In this case, the import statement may be executed many times.
Apart from stylistic issues, what is the performance cost of doing this?
Take a look at the explanation on this site:
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead
Even though Python won't import the same module multiple times, you may still have a performance hit depending on how your code is structured. You may be able to use the Timer to see what the actual impact is.
There's very little cost to a repeated import
statement, since Python caches modules and only imports them once (at the first import
), unless explicitly asked to reload a module with the reload
function. The effect (and rough performance impact) of a repeated import
statement is essentially just binding the imported names in the local namespace.
It isn't completely free, however; import
does have to lock and unlock the import table lock, and resolve the provided names. This means that can still slow down your program if called frequently.
Posting this as an answer as it's too big for a comment:
It was not clear to me that my original timing efforts were working as expected, as I got warnings that the slowest were 20 times slower than the fastest - ie: some caching was occuring to change the results.
The following, run in an ipython notebook with python 2.7.3 does, however, seem to indicate the performance hit of about 450 nano seconds:
%%timeit import numpy
pass
100000000 loops, best of 3: 11.3 ns per loop
%%timeit import numpy
import numpy
pass
1000000 loops, best of 3: 465 ns per loop