DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Understanding Java Signals
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases

An Intro to Flake8

Easily lint your code and enforce PEP8 compliance.

By 
Mike Driscoll user avatar
Mike Driscoll
·
Aug. 15, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

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. 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 less noisy than PyLint. Give it a try and see what you think!


The post An Intro to Flake8 appeared first on The Mouse Vs. The Python.

Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: