What is Flake8, and why should we use it?
Recently I was thinking about tools that make me a better Python Developer, among many valuable and good ones, I’ve decided to start with one which makes my daily routine easier. I will share what linting is, how to use it, and how to incorporate it into your automated build.
What is Lint?
In computer programming, lint or lint-like tools perform static source code analysis, checking for symantec discrepancies.
What is “Linting”?
“Linting” means running a basic quality tool against your code. The tool will check your code syntax and provide instructions on how to clean it.
Why is “linting” important, and why should we use it?
Well… I think it is crucial. One simple reason: linting helps to prevent bugs in your program. In other words:
- Linting makes you a better developer by helping you write better code (checking against coding standards)
- It helps prevent things like syntax errors, typos, bad formatting, incorrect styling, etc
- It saves our time as a developer
- If you are working in a team, it saves time for people who are reviewing your code (no need to distrust for typos and formatting issues)
- It is easy to use
- Lint-like tools usually very easy to setup
- It is free :)
In short, code liners are awesome!
What is Flake8?
There are a couple of good python code linter tools you can use. I’ve recently discovered a Flake8, “the wrapper which verifies pep8, pyflakes, and circular complexity “. It has a low rate of false positives.
Flake8 is easy to setup:
Just run :
python<version> -m pip install flake8
You can easily add it to your python IDE or editor ( e.g., PyCharm, SublimeText, etc.).
Supported by Python2+:
python2.7 -m flake8
and
Python 3+:
python3.5 -m flake8
Flake8 usage:
Flake8 runs all the tools by launching the single flake8 command.
You can run
flake8 — help
for help instructions
Run in your:
flake8 path/to/your_code/main.py #check particular file
or
flake8 path/to/your_project/ #check the entire project repo
Cool features:
One of the useful features I found is the flag to run Flake8 only for specific types of warnings, errors, etc.
For example:
flake8 — select W54 path/to/your_project/
To ignore warnings, you can do the following:
flake8 — ignore W54 path/to/your_project/
After trying flake8 multiple times, you realize that some set of commands with Flake8 is the same. Good thing Flake8 supports creating and storing configuration files. You can change the Flake8 settings globally by editing its config file. The user configuration file is expected to be stored in the user’s “home” directory. Which will read from the ~/.config/flake8 file (~/.flake8 for Windows)
Configure Flake8 with:
Other good linting tools I’ve used before:
Linter is a useful tool, so go and try it yourself. I can assure you will learn more, and it will make you a better Python Programmer with less stress and more time :)
Looking for more details:
Documentation is here
Want to contribute:
Just click on this link for more details