Python has several linters that you can use to help you find errors in your code or warnings about style. For example, one of the most thorough linters is Pylint.
Flake8 is described as a tool for style guide enforcement. It is also a wrapper around PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. You can use Flake8 as a way to lint your code and enforce PEP8 compliance.
Installation
Installing Flake8 is pretty easy when you use pip. If you’d like to install flake8 to your default Python location, you can do so with the following command:
python -m pip install flake8
Now that Flake8 is installed, let’s learn how to use it!
Getting Started
The next step is to use Flake8 on your code base. Let’s write up a small piece of code to run Flake8 against.
Put the following code into a file named hello.py:
from tkinter import * class Hello: def __init__(self, parent): self.master = parent parent.title("Hello Tkinter") self.label = Label(parent, text="Hello there") self.label.pack() self.close_button = Button(parent, text="Close", command=parent.quit) self.close_button.pack() def greet(self): print("Greetings!") root = Tk() my_gui = Hello(root) root.mainloop()
Here you write a small GUI application using Python's tkinter library. A lot of tkinter code on the internet uses the from tkinter import * pattern, which is something that you should normally avoid. The reason being that you don't know everything you are importing and you could accidentally overwrite an import with your own variable name.
Let's run Flake8 against this code example.
Open up your terminal and run the following command:
flake8 hello.py
You should see the following output:
tkkkk.py:1:1: F403 'from tkinter import *' used; unable to detect undefined names
tkkkk.py:3:1: E302 expected 2 blank lines, found 1
tkkkk.py:8:22: F405 'Label' may be undefined, or defined from star imports: tkinter
tkkkk.py:11:29: F405 'Button' may be undefined, or defined from star imports: tkinter
tkkkk.py:18:1: E305 expected 2 blank lines after class or function definition, found 1
tkkkk.py:18:8: F405 'Tk' may be undefined, or defined from star imports: tkinter
tkkkk.py:20:16: W292 no newline at end of file
The items that start with "F" are PyFlake error codes and point out potential errors in your code. The other errors are from pycodestyle. You should check out those two links to see the full error code listings and what they mean.
You can also run Flake8 against a directory of files rather than one file at a time.
If you want to limit what types of errors you want to catch, you can do something like the following:
flake8 --select E123 my_project_dir
This will only show the E123 error for any files that have them in the specified directory and ignore all other types of errors.
For a full listing of the command line arguments you can use with Flake8, check out their documentation.
Finally, Flake8 allows you to change its configuration. For example, your company might only adhere to parts of PEP8, so you don't want Flake8 to flag things that your company doesn't care about. Flake8 also supports using plugins.
Wrapping Up
Flake8 might be just the tool for you to use to help keep your code clean and free of errors. If you use a continuous integration system, like TravisCI or Jenkins, you can combine Flake8 with Black to automatically format your code and flag errors.
This is definitely a tool worth checking out and it's a lot less noisy than PyLint. Give it a try and see what you think!
Related Reading
- Flake8 Website
- pyflakes – the passive checker of Python programs
- Intro to Black – The Uncompromising Python Code Formatter
- PyLint: Analyzing Python Code
Pingback: ICYMI: Consumers Should Immediately… Python the Circuit! #Python #Adafruit #CircuitPython #PythonHardware #ICYMI @circuitpython @micropython @ThePSF @Adafruit « Adafruit Industries – Makers, hackers, artists, designers and engineers!