PEP 08 states:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.<
Module importing is quite fast, but not instant. This means that:
So if you care about efficiency, put the imports at the top. Only move them into a function if your profiling shows that would help (you did profile to see where best to improve performance, right??)
The best reasons I've seen to perform lazy imports are:
__init__.py
of a plugin, which might be imported but not actually used. Examples are Bazaar plugins, which use bzrlib
's lazy-loading framework.