Install the below python packages for Python Code Format supporting in Windows

pip install autopep8

pip install pylint

Once you have successfully installed the packages, Open the code in vs code -> Press Ctrl + A and Ctrl + K, it will format the code.

Answer from Dhandapani Sudhakar on Stack Overflow
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ formatting
Formatting Python in VS Code
November 3, 2021 - Open a Python file in VS Code. Right-click on the editor to display the context menu. Select Format Document With.... Select Configure Default Formatter...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ visualstudio โ€บ python โ€บ formatting-python-code
Reformat Your Python Code in Visual Studio - Visual Studio (Windows) | Microsoft Learn
January 7, 2026 - Automatically format your Python code in Visual Studio, including the code spacing, statements, long line wrapping, and code comments.
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ editing
Editing Python in Visual Studio Code
November 3, 2021 - Missed Agent Sessions Day? Watch on-demand now ยท Visual Studio Code is a powerful editing tool for Python source code. The editor includes various features to help you be productive when writing code. For more information about editing in Visual Studio Code, see Basic Editing and Code Navigation
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Black Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - Formatting support for Python files using the Black formatter.
๐ŸŒ
Dave Gray
davegray.codes โ€บ posts โ€บ how-to-auto-format-unwanted-python-line-indents
How to Auto-Format Unwanted Python Line Indentations in VS Code | Dave Gray
February 10, 2024 - Update: You can install autopep8 in your virtual environment and run autopep8 -i -a -a your-file-name.py to get the desired formatting. Unfortunately, it won't apply in VS Code settings. You must run this in a terminal window. Thanks to Michael J. Sheets for the email! I published a 9 Hour Python Course for Beginners in 2023.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ adamlombard โ€บ how-to-use-the-black-python-code-formatter-in-vscode-3lo0
VSCode: Using Black to automatically format Python - DEV Community
April 4, 2024 - Black is "the uncompromising Python code formatter." It can be configured to automatically format your code whenever you save a file in VSCode.
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Formatter extension for Visual Studio Code using autopep8
Extension for Visual Studio Code - Formatting support for Python files using the autopep8 formatter.
๐ŸŒ
Readthedocs
py-vscode.readthedocs.io โ€บ en โ€บ latest โ€บ files โ€บ linting.html
Linting & Formatting ๐ŸŽ€ โ€” How to Python in VS Code ๐Ÿฆ„ documentation
You have to install flake8 and black in your environment via pip install flake8 and pip install black respectively. If you want to set them up globally and donโ€™t want to worry about formatting ever again, you have set up their global paths. To do so: ... Now go to the settings and search ...
๐ŸŒ
Medium
danlabrador.medium.com โ€บ how-to-set-up-python-auto-formatter-on-save-in-vs-code-52a8ef84e2e4
How to set up Python auto-formatter on save in VS Code | Medium
March 28, 2025 - Learn how to automatically format your Python code and organise imports on save in VS Code. Quick setup guide includedโ€ฆ
๐ŸŒ
DEV Community
dev.to โ€บ eegli โ€บ quick-guide-to-python-formatting-in-vs-code-2040
Quick Guide to Python Formatting in VS Code (2025) - DEV Community
February 1, 2025 - I prefer applying this configuration to my user settings - this way, I can easily start a Python project and will get formatting out of the box based on my user settings. No need to create dedicated workspace settings for every single project and override the formatter over and over. Here's the relevant config from my settings: { "editor.defaultFormatter": "esbenp.prettier-vscode", "[python]": { "editor.defaultFormatter": null }, "python.formatting.blackArgs": ["--line-length", "120"], "python.formatting.provider": "black", }
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-black-formatter
GitHub - microsoft/vscode-black-formatter: Formatting support for Python using the Black formatter ยท GitHub
A Visual Studio Code extension with support for the Black formatter. The extension ships with black=26.1.0. Note: The minimum version of Black this extension supports is 26.1.0. This extension includes support for all actively supported versions ...
Starred by 190 users
Forked by 45 users
Languages ย  Python 88.5% | TypeScript 11.1% | JavaScript 0.4%
๐ŸŒ
DEV Community
dev.to โ€บ jajera โ€บ how-to-configure-vscode-for-auto-formatting-and-linting-in-python-37bc
How to Configure VSCode for Auto Formatting and Linting in Python - DEV Community
January 8, 2025 - Learn how to set up VSCode to automatically format and lint Python code using configuration files and CLI commands, avoiding manual steps.
๐ŸŒ
Orchestra
getorchestra.io โ€บ guides โ€บ how-to-install-black-formatter-on-vs-code
How to Install Black Formatter on VS Code | Orchestra
February 10, 2026 - Learn how to install and configure Black Formatter on VS Code to streamline Python code formatting and ensure consistent code style.
Top answer
1 of 7
18

There are only a few settings you need to setup black as a formatter on VS Code. It seems you got most of it right, but I am doubtful about using relative paths for blackPath (VS Code should have shown an error if the path is indeed incorrect).

I suggest switching to absolute paths.

Here are my settings:

// User Settings

"editor.defaultFormatter": null,
"editor.formatOnSave": false,  // enable per language
"[python]": {
    "editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black"

// Workspace Settings

"python.formatting.blackPath": "/absolute/path/to/venv/with/black",
"python.formatting.blackArgs": [
    "-l 120"
],

First of all, I suggest getting rid of the editor.defaultFormatter setting (or just set it back to the default null). Instead of setting a default for everything, configure your formatter for each language and for each extension. Here, it's null and then I configure python-specific settings then I have separate ones for other languages (ex. JS and C++). You mentioned something about Prettier, and that could be interfering with VS Code using black.

Second, make sure you are modifying the correct settings. VS Code has 3 sets of settings: User, Workspace, and Folder. I normally have the formatOnSave enabled for Python on the User settings, and provider set to black (using system-wide installed black). On a specific workspace, I have a virtual environment and I override the blackPath to the black specifically installed on that virtual environment. You can also just put all the settings in the User settings or use the same system-wide-installed black. But the main point here is to use absolute paths for both (basically copying the output of which black from the console).

Note that, if you specified blackPath to point to a particular virtual environment, make sure to select that same virtual environment on your workspace.

Lastly, you can check for any issues from the Output tab > Python:

2 of 7
2

I was also having the issue. What solved the issue for me was after downloading the Black formatter extension and once the document was open I did a right-click with the mouse and then press "format document". Choose black. I was good to go and had no more issues after that.