Last modified: Oct 02, 2026

How to Install Flake8 in Python

Flake8 is a popular linting tool for Python. It checks your code for style errors and potential bugs. This guide shows you how to install Flake8 quickly and correctly.

You will learn to use pip, set up a virtual environment, and run your first check. The steps work on Windows, macOS, and Linux.

What Is Flake8 and Why Use It?

Flake8 combines several tools: PyFlakes, pycodestyle, and McCabe. It reports unused imports, missing whitespace, and complex functions.

Using Flake8 helps you write clean and readable Python code. It catches mistakes before they reach production.

Many teams enforce Flake8 in their continuous integration pipelines. Installing it early saves time and reduces code review friction.

Prerequisites Before Installing Flake8

You need Python installed on your system. Flake8 supports Python 3.6 and newer. Check your version with the following command.


python --version

If you see Python 3.6 or higher, you are ready. Also ensure pip is available. Run this command to check.


pip --version

Most Python installations include pip by default. If not, install pip first using your system package manager.

Method 1: Install Flake8 with pip (Global)

The simplest way is to use pip directly. Open your terminal or command prompt.

Run the following command. This installs Flake8 for your current Python interpreter.


pip install flake8

On some systems, you may need to use pip3 instead. Try this if the first command fails.


pip3 install flake8

Wait for the download and installation to finish. You should see a success message.

Global installation works for quick tests. But for real projects, a virtual environment is better.

Method 2: Install Flake8 in a Virtual Environment

A virtual environment isolates packages per project. This prevents version conflicts.

First, create a virtual environment. Navigate to your project folder and run:


python -m venv venv

This creates a folder named venv. Now activate it.

On Windows, use this command:


venv\Scripts\activate

On macOS or Linux, use this command:


source venv/bin/activate

Your prompt will change to show the active environment. Now install Flake8 inside it.


pip install flake8

This keeps Flake8 local to your project. Other projects remain unaffected.

Method 3: Install Flake8 with conda

If you use Anaconda or Miniconda, you can install Flake8 with conda.

Run this command in your terminal:


conda install flake8

Conda resolves dependencies automatically. It works well for data science environments.

After installation, verify Flake8 is available. We cover verification next.

How to Verify Flake8 Installation

Check that Flake8 is installed and accessible. Run this command.


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

The exact version numbers may differ. The important part is that the command runs without errors.

If you see "command not found", Flake8 is not on your PATH. Try reinstalling or check your environment.

Running Your First Flake8 Check

Create a simple Python file to test. Name it example.py.


import os

def  my_function( x ):
    return x+1

print( my_function( 5 ) )

This code has style issues: extra spaces, missing spaces around operators, and an unused import.

Now run Flake8 on this file.


flake8 example.py

You will see output listing each problem. Here is an example output.


example.py:1:1: F401 'os' imported but unused
example.py:3:1: E302 expected 2 blank lines, found 1
example.py:3:5: E211 whitespace before '('
example.py:3:20: E202 whitespace before ')'
example.py:4:12: E225 missing whitespace around operator
example.py:6:7: E201 whitespace after '('
example.py:6:22: E202 whitespace before ')'

Each line shows the file, line number, column, and error code. Fix these issues to make your code cleaner.

Flake8 does not fix code automatically. You must edit the file manually or use a formatter like Black.

Common Flake8 Configuration Options

You can customize Flake8 with a configuration file. Create a file named .flake8 or setup.cfg in your project root.

Here is an example configuration.


[flake8]
max-line-length = 88
ignore = E203, W503
exclude = .git,__pycache__,venv

This sets a line length of 88 characters, ignores two common style rules, and excludes certain folders.

Adjust these settings to match your team's style guide. The max-line-length option is very common.

Troubleshooting Installation Issues

Sometimes installation fails. Here are common problems and fixes.

Permission denied: Use pip install --user flake8 or run as administrator.

Old pip version: Upgrade pip with python -m pip install --upgrade pip.

Multiple Python versions: Use python -m pip install flake8 to target the correct interpreter.

Virtual environment not activated: Check your prompt. Reactivate the environment if needed.

If you still have issues, try uninstalling and reinstalling Flake8.


pip uninstall flake8
pip install flake8

Integrating Flake8 with Your Editor

Most code editors support Flake8. For example, VS Code has a Python extension.

Enable linting in settings. Set the linter to flake8. The editor will show errors as you type.

PyCharm also supports Flake8. Go to Settings, then Tools, then External Tools. Add Flake8 as a tool.

Using Flake8 in your editor catches issues immediately. This saves time compared to running it manually.

Conclusion

Installing Flake8 in Python is straightforward. Use pip for global installs or virtual environments for isolated projects.

Verify the installation with flake8 --version. Run your first check on a test file to see it in action.

Configure Flake8 to match your style guide. Integrate it with your editor for real-time feedback.

Flake8 helps you write cleaner, more professional Python code. Install it today and start improving your code quality.