Flake8 isort как установить

от admin

Setup black, isort, flake8 in VSCode

After you completed this article, the Python code will be auto formatting and linting whenever you save the file in VSCode.

VSCode Python Extension

Please make sure you have installed the python extension in the VSCode. You can install it through the UI.

Or type the “Ctrl + p” or “Cmd + p” (Based on you are using the Windows or MacOS). Paste the below line and hit enter to install.

Black

First, install the black into your Python environment by pip.

Type the “Ctrl + ,” or “Cmd + ,” to open the settings page in the VSCode. (You can also find it through UI “Preferences -> Settings”)

Search “format on save” and check the checkbox.

Search “python formatting provider” and select the “black”.

Now, if you open the python file and save, it will be auto formatted by the black!

For example, if your file is like this before:

After save, it will be like this:

isort

Type “Ctrl + Shift + P” or “Cmd + Shift + P”. Search the “Preferences: Configure Language Specific Settings” and press enter. And search the “Python” and press enter. It will open the “settings.json”.

Please add the following settings:

Therefore, it may look like this:

(VSCode already has the built-in function to use isort so that we don’t need to install it by pip)

Then isort will reorder your import order when you save the file. For example, if the file you have is like this:

After save, it will be like this:

flake8

First, install the flake8 into your Python environment by pip.

Type “Ctrl + Shift + P” or “Cmd + Shift + P”. Search the “Python: Select Linter” and press enter. Select the “flake8”

How to Protect Your Python Code Health ����

Code can be long and complex. Let’s configure a few good tools to help keep our Python codebase healthy. Flake8, Black, and isort.

Linting and Formatting

Flake8 is a linter. It’s designed to find mistakes in your code that can later become bugs when your code is run.

A formatter arranges our code so that it’s more readable on the screen, but does not change what our code does. Black and isort are formatters.

Installation

Let’s install our linting and formatting tools. For a healthy coding environment use a virtual environment like venv or virtualenv ( venv comes with Python).

Create a virtual environment and activate it.

Exit fullscreen mode

Install the packages.

Exit fullscreen mode

Configuration

You’ll want to configure these tools a little to make them play nicely together.

Flake8 ��

There’s a lot you can configure with Flake8, but you don’t have to. Add the bare minimum first to get Black and Flake8 to cooperate (also we’ll exclude a few directories from linting). In the root of your project create a file called .flake8 .

Exit fullscreen mode

Black ��️

Black is famous as a highly opinionated «uncompromising code formatter». While there are a few settings you can configure, nearly all of the formatting choices are made for you. That’s actually half of its appeal. The other half is getting back all the time you previously used to tweak where this comma was or that spacing. Embrace the way Black will do it all for you and you’ll thank me later.

In the root of your project create a file called pyproject.toml .

Exit fullscreen mode

isort ��️

Short for «import sort» or just «I Sort», this utility will arrange your module’s imports in a consistent opinionated way, much like Black does for the rest of your code.

Let’s configure isort to play nicely with Black. Add the following to our pyproject.toml .

Exit fullscreen mode

Checking Your Code’s Health ��

Let’s run our tools individually first.

Exit fullscreen mode

Now let’s make this a little easier for ourselves by setting up a pre-commit hook. In your project create a file called .git/hooks/pre-commit .

Exit fullscreen mode

When you run git commit this script will run and check your code health before a commit is made. It will show you what issues Flake8 has found and it will format your code. You’ll then get to go back and fix the issues. Finally, you’ll run git commit again.

Here’s what it looks like with errors.

Exit fullscreen mode

Here’s what it will look like when you’re code is healthy.

Exit fullscreen mode

Automation ��

With code health, the more often you can check it the better. You’ve seen how to setup a Git hook. Here’s a few other ways to run Flake8, Black, and isort.

Your code editor likely integrates with these tools using only some light configuration. You’ll benefit from spellcheck-like behavior from Flake8, and format on save from Black and isort.

Some info on setting up VSCode for linting and formatting.

Pre-Commit

The Pre-Commit Framework is Git hooks with super powers. Its easy setup and configuration give you access to an extensive list of shared Git hooks.

CI/CD Pipelines

While a Git hook is great for catching errors before they go to GitHub/GitLab you still want the safety of your CI/CD Pipeline running these tools again. That’s because sometimes people forget to configure their Git hooks ��.

For our formatters, there is a special CI/CD mode that will throw an error when the formatting is incorrect.

Exit fullscreen mode

Source Code Example ��

You’re all set with the tools you’ll need to keep your code healthy. Remember the goal is to make our code bug free, and easy to maintain. Flake8, Black, and isort can help you get there.

Check out packagecake for an example of what we’ve covered here.

sirfuzzalot / packagecake

Turn your Python package into a delicious cake ��

Find out what type of cake your Python package is by running the following:

How to use black, flake8, isort, and pre-commit framework to format Python codes

With black you can format Python code from 2.7 all the way to 3.8 (as of version 20.8b1), which makes for a great replacement for YAPF which can only format code depending on the Python version being used to run it.

My preference is using PEP 8 as my style guide, and so, 79-characters per line of code is what I use. So it’s as simple as running the following code at the root of my project and all non-compliant files will be reformatted:

Let’s explain each option.

  • -l or —line-length : How many characters per line to allow. [default: 88]
  • -t or —target-version : Python versions that should be supported by Black’s output. [default: per-file auto-detection]

Fairly simple. Allow 79 characters per line, and use py27 as the targetted version.

isort: A Python library to sort imports.

And just as their slogan states: “isort your imports, so you don’t have to.”

The options used are mainly to be compatible with black (see here):

  • —multi-line : Multiline output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, 5-vert-grid-grouped, 6-vert-grid-grouped-no-comma, 7-noqa, 8-vertical-hanging-indent-bracket, 9-vertical-prefix-from-module-import, 10-hanging-indent-with-parentheses).
    • 3-vert-hanging
    • black
    • 27 for Python 2.7

    But there’s still something missing. black does not care about comments or docstrings, and isort cares even less, for obvious reasons; enter flake8 .

    flake8: A python tool that glues together pep8, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code

    Anthony Sottile (@asottile) has mentioned that he plans to drop support for Python 2.7 in future releases, maybe in version 3.9 or 4.0.

    Fortunately, I can still use it for Python 2 by running the following command:

    PEP 8 recommends limiting docstrings or comments to 72 characters, which is exactly what I’m using for flake8.

    So let’s explain each option used.

    • —max-doc-length : Maximum allowed doc line length for the entirety of this run. (Default: None)
    • —ignore : Comma-separated list of errors and warnings to ignore (or skip). For example, —ignore=E4,E51,W234 . (Default: [‘E226’, ‘E123’, ‘W504’, ‘E121’, ‘W503’, ‘E126’, ‘E704’, ‘E24’])

    In my case, I am using 72 as the maximum allowed characters for my docstrings, in accordance with PEP 8, and ignoring the following errors:

Похожие статьи