The current version of pylint now does this, and reports it as error class C0411.
Import order coding standard
-
23-06-2023 - |
Вопрос
PEP8 suggests that:
Imports should be grouped in the following order:
- standard library imports
- related third party imports
- local application/library specific imports
You should put a blank line between each group of imports.
Is there a way to check if the standard is violated anywhere in the package using static code analysis tools, like pylint
, pyflakes
, pychecker
, pep8
?
Example of violation:
from my_package import my_module
from django.db import models
import os
Correct way to import:
import os
from django.db import models
from my_package import my_module
Решение
Другие советы
Update (2016): sbywater has the most recent answer.
Found it! (accidentally, while reading "Hacker's guide to python")
OpenStack Hacking Style Checks project named hacking introduces several unique flake8
extensions. There is hacking_import_groups among them (related commit).
Example:
requirements
files used in the example
tox.ini
(we need to tell flake8 that we want to use a custom check)[hacking] local-check = hacking.core.hacking_import_groups
UPD: with the newest version of
hacking
the path to the check changed, now it ishacking.checks.imports.hacking_import_groups
.test.py
(target of the check)import requests import sys from my_module import print_smth print_smth(requests.get('https://google.com')) print_smth(sys.version)
my_module.py
(local import used bytest.py
)def print_smth(smth): print smth
Then, if I run flake8
against test.py
:
$ flake8 test.py
test.py:2:1: H305 imports not grouped correctly (requests: third-party, sys: stdlib)
test.py:3:1: H305 imports not grouped correctly (sys: stdlib, my_module.print_smth: project)
test.py:3:1: H306 imports not in alphabetical order (sys, my_module.print_smth)
Then, if I group the imports in the correct order following PEP8
:
import sys
import requests
from my_module import print_smth
print_smth(requests.get('https://google.com'))
print_smth(sys.version)
No warnings found:
$ flake8 test.py
$
Hope this will help somebody in the future.
Have a look at https://pypi.python.org/pypi/isort or https://github.com/timothycrosley/isort
isort parses specified files for global level import lines (imports outside of try / excepts blocks, functions, etc..) and puts them all at the top of the file grouped together by the type of import:
- Future
- Python Standard Library
- Third Party
- Current Python Project
- Explicitly Local (. before import, as in: from . import x)
Custom Separate Sections (Defined by forced_separate list in configuration file) Inside of each section the imports are sorted alphabetically. isort automatically removes duplicate python imports, and wraps long from imports to the specified line length (defaults to 80).
https://pypi.python.org/pypi/flake8-isort plugs this functionality into flake8
A flake8
plugin exists: flake8-import-order.
This package adds 3 new flake8 warnings
I100: Your import statements are in the wrong order.
I101: The names in your from import are in the wrong order.
I201: Missing newline between sections or imports.
Broadly, You need to structure your import statements to meet pep8 import standard.
I found isort library useful in ordering import statements to get rid of flake8 ordering issues.
pip install isort
isort <your_python_file>.py
The Flake8 plugin Alphabetize https://github.com/tlocke/flake8-alphabetize checks import sort order and is designed to work with the Black formatter. It follows the Black formatter approach of having a single style, with no configuration of the style possible. Alphabetize also checks the order of the __all__
attribute.