module-import-not-at-top-of-file (E402)#
Derived from the pycodestyle linter.
What it does#
Checks for imports that are not at the top of the file. For Jupyter notebooks, this checks for imports that are not at the top of the cell.
Why is this bad?#
According to PEP 8, "imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants."
This rule makes an exception for sys.path
modifications, allowing for
sys.path.insert
, sys.path.append
, and similar modifications between import
statements.
In preview, this rule also allows os.environ
modifications between import
statements.
Example#
Use instead: