Last modified: Oct 02, 2026
How to Use Flake8 to Lint Python Code
Writing Python code is easy. Writing clean Python code takes practice.
That is where Flake8 comes in. Flake8 is a popular linting tool for Python. It checks your code for style issues, syntax errors, and possible bugs.
In this guide, you will learn what Flake8 is, how to install it, and how to use it in your daily workflow. By the end, you will be able to lint any Python project with confidence.
What Is Flake8?
Flake8 is a command-line tool that checks your Python source code. It combines three tools into one: PyFlakes, pycodestyle, and McCabe.
PyFlakes finds logical errors, like unused imports or undefined names. pycodestyle checks your code against PEP 8, the official Python style guide. McCabe measures code complexity.
Together, these tools help you catch problems before they reach production. Flake8 is fast, lightweight, and easy to configure.
Why Use Flake8?
Clean code is easier to read, review, and maintain. Flake8 helps you achieve that in several ways.
First, it catches real bugs. An unused variable or a missing import can cause confusing errors later. Flake8 flags these issues early.
Second, it enforces a consistent style. When everyone on a team follows PEP 8, the codebase feels uniform. Reviews become faster and friendlier.
Third, it saves time. Instead of arguing about formatting in pull requests, your team can let Flake8 handle it.
Finally, it integrates with most editors and CI pipelines. That means you get feedback as you type or before you merge.
How to Install Flake8
Flake8 is available on PyPI. You can install it with pip.
pip install flake8
If you use a virtual environment, activate it first. This keeps Flake8 isolated to your project.
To confirm the installation, check the version.
flake8 --version
You should see output like this.
6.1.0 (mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.1.0) CPython 3.11.4 on Linux
Running Flake8 on a File
The simplest way to use Flake8 is to point it at a file.
flake8 my_script.py
If the file is clean, Flake8 prints nothing. Silence means success.
If there are issues, Flake8 lists them with file name, line number, column, and an error code.
my_script.py:3:1: F401 'os' imported but unused
my_script.py:7:80: E501 line too long (92 > 79 characters)
Each code has a meaning. F401 means an unused import. E501 means a line is too long. You can look up any code in the Flake8 documentation.
Running Flake8 on a Project
You can also lint an entire directory. Just pass the folder name.
flake8 my_project/
Flake8 walks through every Python file it finds. It skips hidden folders and common virtual environment directories by default.
For larger projects, you may want to exclude folders like migrations or build artifacts. You can do that with a configuration file.
Configuring Flake8
Flake8 reads settings from several places. The most common options are setup.cfg, tox.ini, and .flake8.
Here is a simple example using setup.cfg.
[flake8]
max-line-length = 100
exclude = .git,__pycache__,build,dist
ignore = E203,W503
The max-line-length option changes the default 79 characters to 100. The exclude option tells Flake8 which folders to skip. The ignore option silences specific error codes.
Be careful with ignore. Only silence codes when you have a good reason. Otherwise, you may hide real problems.
Ignoring Specific Lines
Sometimes you need to break a rule on purpose. You can add a noqa comment to a line.
import os # noqa: F401
The noqa comment tells Flake8 to skip that line. You can also target a specific code, as shown above.
Use this sparingly. Too many noqa comments make your code harder to trust.
Integrating Flake8 with Editors
Flake8 works well with most code editors. Popular choices include VS Code, PyCharm, and Vim.
In VS Code, you can install the Python extension. It runs Flake8 automatically and shows errors in the Problems panel.
In PyCharm, you can enable Flake8 as an external tool. PyCharm also has built-in PEP 8 checks that overlap with Flake8.
Real-time feedback is powerful. You fix issues as you write, not after.
Using Flake8 in CI Pipelines
Linting should not depend on humans remembering to run it. Add Flake8 to your CI pipeline.
Here is a simple GitHub Actions step.
- name: Lint with Flake8
run: |
pip install flake8
flake8 .
If Flake8 finds issues, the build fails. That forces the team to fix problems before merging.
This small step prevents style drift over time. It also keeps your main branch clean.
Common Flake8 Error Codes
Here are some codes you will see often.
E501: line too long. Break the line or shorten it.
E302: expected two blank lines. Add a blank line before a function or class.
F401: unused import. Remove it or use it.
F841: local variable assigned but never used. Delete it or use it.
W291: trailing whitespace. Most editors can strip this automatically.
Learning these codes helps you fix issues faster. Over time, you will recognize them at a glance.
Flake8 vs Other Linters
Flake8 is not the only linter for Python. Others include Pylint, Ruff, and Black.
Pylint is more opinionated and slower. It checks more rules but can be noisy.
Ruff is a newer tool written in Rust. It is extremely fast and replaces several tools, including Flake8.
Black is a formatter, not a linter. It rewrites your code to match a style. Many teams use Black and Flake8 together.
Flake8 remains a solid choice. It is mature, well documented, and widely supported.
Best Practices for Using Flake8
Start with the defaults. Run Flake8 on your project and see what it finds.
Fix the easy issues first. Unused imports and long lines are quick wins.
Then add a config file. Set a line length that fits your team and exclude folders you do not care about.
Add Flake8 to your editor and CI. Automation beats discipline every time.
Finally, do not ignore every warning. Each code exists for a reason. Understand it before you silence it.
Conclusion
Flake8 is a simple but powerful tool for linting Python code. It catches bugs, enforces style, and keeps your codebase healthy.
Install it with pip, run it on files or folders, and configure it to match your project. Then plug it into your editor and CI pipeline.
With a few small steps, you will write cleaner Python and spend less time fixing avoidable mistakes. Give Flake8 a try on your next project.