flake8 in Python
| Introduction | |
| Install | |
| Usage | |
| Only on Changed Files | |
| Related Articles |
Introduction
Flake8 - is a tool that checks Python code for PEP8 compliance and some errors. It is essentially a wrapper for the following tools:
- PyFlakes
- pycodestyle
- Ned Batchelder’s McCabe script
Installation
To install flake8 run the command
python -m pip install flake8
Collecting flake8 Using cached flake8-7.2.0-py2.py3-none-any.whl.metadata (3.8 kB) Collecting mccabe<0.8.0,>=0.7.0 (from flake8) Using cached mccabe-0.7.0-py2.py3-none-any.whl.metadata (5.0 kB) Collecting pycodestyle<2.14.0,>=2.13.0 (from flake8) Using cached pycodestyle-2.13.0-py2.py3-none-any.whl.metadata (4.5 kB) Collecting pyflakes<3.4.0,>=3.3.0 (from flake8) Using cached pyflakes-3.3.2-py2.py3-none-any.whl.metadata (3.5 kB) Using cached flake8-7.2.0-py2.py3-none-any.whl (57 kB) Using cached mccabe-0.7.0-py2.py3-none-any.whl (7.3 kB) Using cached pycodestyle-2.13.0-py2.py3-none-any.whl (31 kB) Using cached pyflakes-3.3.2-py2.py3-none-any.whl (63 kB) Installing collected packages: pyflakes, pycodestyle, mccabe, flake8 Successfully installed flake8-7.2.0 mccabe-0.7.0 pycodestyle-2.13.0 pyflakes-3.3.2
Example of usage
You can apply flake8 to a specific file or to a whole directory
python -m flake8 app/lib
.\app\lib\InfoPage.py:89:80: E501 line too long (270 > 79 characters) .\app\lib\InfoPage.py:75:1: E302 expected 2 blank lines, found 1 .\app\lib\local_keywords.py:95:1: W391 blank line at end of file
Apply to changed files only
To use flake8 against files that were not yet commited, including those in staging area
python -m flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
| Basics | |
| Python | |
| Installation | |
| OOP in Python | |
| flake8 | |
| docstring | |
| #!: Shebang | |
| Objects | |
| os | |
| pathlib | |
| flake8 |