Suppose I have a relatively long module, but need an external module or method only once.
Is it considered OK to import that method or module in the middle of the module?
Or should import
s only be in the first part of the module.
Example:
import string, pythis, pythat
...
...
...
...
def func():
blah
blah
blah
from pysomething import foo
foo()
etc
etc
etc
...
...
...
Please justify your answer and add links to PEPs or relevant sources
PEP 8 authoritatively states:
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
PEP 8 should be the basis of any "in-house" style guide, since it summarizes what the core Python team has found to be the most effective style, overall (and with individual dissent of course, as on any other language, but consensus and the BDFL agree on PEP 8).