Use isort to check if the imports on your python files are sorted the way you expect.
As an alternative to this flake8 plugin, there’s flake8-import-order that could be worth checking out. In contrast to this plugin that defers all logic to isort, the flake8-import-order comes bundled with it’s own logic.
flake8-import-order comes with a few predefined set of styles meanwhile this plugin can be customized a bit more. But the biggest difference could lie in that flake8-isort actually has the corresponding sorting engine isort that can sort the import orders of your existing python files. Meanwhile flake8-import-order has no such corresponding tool, hence big existing projects who want to adopt either would get a more automized experience choosing flake8-isort.
0.1.post0 (2015-09-13)
0.1 (2015-09-13)
-
Initial release
[gforcada]
-
Add all boilerplate files.
[gforcada]
-
Create the flake8 plugin per se.
[gforcada]
Источник
Flake8 meet isort
Use isort to check if the imports on your python files are sorted the way you expect.
Add an .isort.cfg to define how you want your imports sorted and run flake8 as you usually do.
See isort documentation for .isort.cfg available options.
Install
Install with pip:
$ pip install flake8-isort
Install with conda:
$ conda install -c conda-forge flake8-isort
Configuration
If using the select option from flake8 be sure to enable the I category as well, see below for the specific error codes reported by flake8-isort.
See flake8 --help for available flake8-isort options.
Error codes
| Error code |
Description |
| I001 |
isort found an import in the wrong position |
| I002 |
no configuration found (.isort.cfg or [isort] in configs) |
| I003 |
isort expected 1 blank line in imports, found 0 |
| I004 |
isort found an unexpected blank line in imports |
| I005 |
isort found an unexpected missing import |
Requirements
- Python 3.7, 3.8, 3.9, 3.10, 3.11 and pypy3
- flake8
- isort
Relation to flake8-import-order
As an alternative to this flake8 plugin, there’s flake8-import-order that could be worth checking out. In contrast to this plugin that defers all logic to isort, the flake8-import-order comes bundled with it’s own logic.
flake8-import-order comes with a few predefined set of styles meanwhile this plugin can be customized a bit more. But the biggest difference could lie in that flake8-isort actually has the corresponding sorting engine isort that can sort the import orders of your existing python files. Meanwhile flake8-import-order has no such corresponding tool, hence big existing projects who want to adopt either would get a more automized experience choosing flake8-isort.
License
GPL 2.0
Источник
Nov 21, 2021
1 min read
Use isort to check if the imports on your python files are sorted the way you expect.
Add an .isort.cfg to define how you want your imports sorted and run flake8 as you usually do.
See isort documentation for .isort.cfg available options.
Install
Install with pip:
$ pip install flake8-isort
Install with conda:
$ conda install -c conda-forge flake8-isort
Configuration
If using the select [option from flake8](https://flake8.pycqa.org/en/latest/user/options.html#cmdoption-flake8-select) be sure to enable the I category as well, see below for the specific error codes reported by flake8-isort.
Error codes
| Error code |
Description |
| I001 |
isort found an import in the wrong position |
| I002 |
no configuration found (.isort.cfg or [isort] in configs) |
| I003 |
isort expected 1 blank line in imports, found 0 |
| I004 |
isort found an unexpected blank line in imports |
| I005 |
isort found an unexpected missing import |
Requirements
- Python 2.7, 3.5, 3.6, pypy or pypy3
- flake8
- isort
Relation to flake8-import-order
As an alternative to this flake8 plugin, there’s flake8-import-order that could be worth checking out. In contrast to this plugin that defers all logic to isort, the flake8-import-order comes bundled with it’s own logic.
flake8-import-order comes with a few predefined set of styles meanwhile this plugin can be customized a bit more. But the biggest difference could lie in that flake8-isort actually has the corresponding sorting engine isort that can sort the import orders of your existing python files. Meanwhile flake8-import-order has no such corresponding tool, hence big existing projects who want to adopt either would get a more automized experience choosing flake8-isort.
License
GPL 2.0
GitHub
https://github.com/gforcada/flake8-isort
John was the first writer to have joined pythonawesome.com. He has since then inculcated very effective writing and reviewing culture at pythonawesome which rivals have found impossible to imitate.
Previous Post
Mypy stubs, i.e., type information, for numpy, pandas and matplotlib
Next Post
Check for python builtins being used as variables or parameters
Источник
Hi,
I’m using flake8-isort plugin in my projects and since isort released the new version, v5.0.0, the plugin stopped working.
Here is the log with traceback:
Traceback (most recent call last):
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 157, in load_plugin
self._load()
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 134, in _load
self._plugin = self.entry_point.load()
File "/app/.tox/lint/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 105, in load
module = import_module(match.group('module'))
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/.tox/lint/lib/python3.6/site-packages/flake8_isort.py", line 3, in <module>
from isort import SortImports
ImportError: cannot import name 'SortImports'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/.tox/lint/bin/flake8", line 8, in <module>
sys.exit(main())
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/cli.py", line 22, in main
app.run(argv)
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 360, in run
self._run(argv)
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 347, in _run
self.initialize(argv)
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 328, in initialize
self.find_plugins(config_finder)
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 159, in find_plugins
self.check_plugins.load_plugins()
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 415, in load_plugins
plugins = list(self.manager.map(load_plugin))
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 302, in map
yield func(self.plugins[name], *args, **kwargs)
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 413, in load_plugin
return plugin.load_plugin()
File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 164, in load_plugin
raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "I00" due to cannot import name 'SortImports'.
Подробнее ...