Skip to content

VS Code Black Args: Auto-format Python line length | ShillehTek

October 22, 2023

Video Tutorial (Optional)

Watch first if you want to follow along and update your VS Code settings for Black Args in real time.

Project Overview

Visual Studio Code + Black Args (Black formatter): In this tutorial, you configure VS Code to run Black with custom arguments so your Python files auto-format to a specific line length when you save.

This is especially useful in larger or older projects where manually reformatting long lines would be tedious.

  • Time: 5 to 10 minutes
  • Skill level: Beginner
  • What you will build: A VS Code setup that formats Python files on save using Black Args with a PEP 8-friendly line-length rule

Parts List

From ShillehTek

  • None required for this software setup

External

Note: This guide updates your VS Code settings.json to use the Black formatter and pass a custom --line-length argument.

Step-by-Step Guide

Step 1 - Install Black

Goal: Install the Black formatter so VS Code can format your Python code.

What to do: In your terminal, install Black with pip:

Code:

pip install black

Expected result: Black is installed in your environment and ready to be used by VS Code.

Step 2 - Open your VS Code settings.json

Goal: Access the JSON settings file where you can configure Python formatting behavior.

What to do: Open the Command Palette.

  • Mac: Command + Shift + P
  • Windows: Control + Shift + P

Type settings and select Preferences: Open Settings (JSON).

VS Code Command Palette showing the option Preferences: Open Settings (JSON)
Open your Settings (JSON) from the Command Palette.

Expected result: You can see and edit your VS Code settings.json file.

Step 3 - Add Black Args to settings.json

Goal: Configure VS Code to use Black and enforce a specific max line length (PEP 8 style).

What to do: Add the following configuration to your settings.json. This example sets a line length of 79 characters.

Code:

{
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "--line-length=79"
  ],
  "python.linting.enabled": true,
  "editor.formatOnSave": true
}

Expected result: VS Code is set to format Python using Black with the custom --line-length argument.

Step 4 - Format a Python file by saving

Goal: Confirm that formatting runs automatically on save.

What to do: Open any Python file in your project and press save. With editor.formatOnSave enabled, Black should reformat the file automatically, including wrapping long lines to match your configured length.

Expected result: When you save a Python file, long lines are auto-formatted, which can save significant time when refactoring larger projects.

Conclusion

You configured Visual Studio Code to use Black Args so Python files auto-format on save with a specific line length setting aligned with PEP 8. This helps you refactor and standardize code faster, especially in large or legacy projects.

Want parts and tools for your next build? Shop at ShillehTek.com. If you want help setting up a clean developer workflow, automating your tooling, or building a custom IoT or software project, check out our consulting services.