[New-bugs-announce] [issue38297] Imports at top of module is often not used
report at bugs.python.org
Fri Sep 27 14:16:34 EDT 2019
New submission from Dominic Littlewood <11dlittlewood at gmail.com>:
In PEP 8, it is stated that:
"Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants."
Note the word "always". This advice makes sense because it cuts down on performance costs.
I recently got into an argument about this, and so I created a script to find all the times in the standard library that an import statement was used inside a function or method. I was expecting to get one or two, but it's safe to say that the answer 1576 was a little surprising. (I was on 3.7.4, if anyone wants to check.)
It turns out that more than one in five modules (defined as any .py file) contain at least one violation of this rule. In the standard library, there are more violations of the rule than there are while loops. Do we need to either a) fix large parts of the standard library or b) rewrite PEP 8 to say that this might not actually be all that bad?
components: Library (Lib)
title: Imports at top of module is often not used
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce