Skip to content

Formatter

Level: Starter☕

black and isort is used as the code formatter.

black

"Black is the uncompromising Python code formatter."

Invoke

Using black is as simple as

black {source_file_or_directory}

Configuration

https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file

Practice:

  • Change the line-length option of black using:
    • Command-line argument
    • Configuration file

Configure Your Editor Using Black

https://black.readthedocs.io/en/stable/integrations/editors.html

For example, this is my current settings.json of VS Code for python

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.formatting.blackArgs": [
        "--line-length",
        "88"
    ],
    "python.formatting.provider": "black",
    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    },
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true
}

isort

"isort your imports, so you don't have to."

https://pycqa.github.io/isort/#using-isort

isort mypythonfile.py mypythonfile2.py
# or
isort .