This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author plokmijnuhby
Recipients plokmijnuhby
Date 2019-09-27.18:16:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1569608194.16.0.811329598218.issue38297@roundup.psfhosted.org>
In-reply-to
Content
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?
History
Date User Action Args
2019-09-27 18:16:34plokmijnuhbysetrecipients: + plokmijnuhby
2019-09-27 18:16:34plokmijnuhbysetmessageid: <1569608194.16.0.811329598218.issue38297@roundup.psfhosted.org>
2019-09-27 18:16:34plokmijnuhbylinkissue38297 messages
2019-09-27 18:16:33plokmijnuhbycreate