The (previously) top-voted answer to this question is nicely formatted but absolutely wrong about performance. Let me demonstrate
Performance
Top Import
import random
def f():
L = []
for i in xrange(1000):
L.append(random.random())
for i in xrange(1000):
f()
$ time python import.py
real 0m0.721s
user 0m0.412s
sys 0m0.020s
Import in Function Body
def f():
import random
L = []
for i in xrange(1000):
L.append(random.random())
for i in xrange(1000):
f()
$ time python import2.py
real 0m0.661s
user 0m0.404s
sys 0m0.008s
As you can see, it can be more efficient to import the module in the function. The reason for this is simple. It moves the reference from a global reference to a local reference. This means that, for CPython at least, the compiler will emit LOAD_FAST
instructions instead of LOAD_GLOBAL
instructions. These are, as the name implies, faster. The other answerer artificially inflated the performance hit of looking in sys.modules
by importing on every single iteration of the loop.
As a rule, it’s best to import at the top but performance is not the reason if you are accessing the module a lot of times. The reasons are that one can keep track of what a module depends on more easily and that doing so is consistent with most of the rest of the Python universe.